0
    var keys = {};
    source.replace(
            /([^=&]+)=([^&]*)/g,
            function(full, key, value) {
                keys[key] =
                        (keys[key] ? keys[key] + "," : "") + value;
                return "";
            }
    );
    var result = [];
    for (var key in keys) {
        result.push(key + "=" + keys[key]);
    }
    return result.join("&");
    }
    alert(compress("foo=1&foo=2&blah=a&blah=b&foo=3"));

我仍然对此感到困惑 /([^=&]+)=([^&]*)/g , + 和 * 用于?

4

2 回答 2

1

^ 表示不是这些,+ 表示匹配的一个或多个字符,() 是组。* 是任意数量的匹配项 (0+)。

http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

因此,通过查看它,我猜它会替换任何不是 =&=& 或 &=& 或 == 的东西,这很奇怪。

于 2013-08-12T02:05:05.047 回答
0

+并且*被称为量词。[]它们确定子集匹配(通常在它们之前的字符集通常与量词组合或()应用量词)可以重复多少次。

/     start of regex

 (    group 1 starts
   [^ anything that does not match
   =& equals or ampersand
   ]+ one or more of above
 )    group 1 ends

   =  followed by equals sign followed by

 (    group 2 starts
   [^ anything that does not match
   =& ampersand
   ]* zero or more of above
 )    group 2 ends

/     end of regex
于 2013-08-12T02:09:02.640 回答