Javascript global match with capturing groups
This question already has an answer here:
As per MDN docs :
If the regular expression does not include the g flag, returns the same result as RegExp.exec(). The returned Array has an extra input property, which contains the original string that was parsed. In addition, it has an index property, which represents the zero-based index of the match in the string.
If the regular expression includes the g flag, the method returns an Array containing all matched substrings rather than match objects. Captured groups are not returned. If there were no matches, the method returns null.
If you want to obtain capture groups and the global flag is set, you need to use RegExp.exec() instead.
var myRe = /(d)(d)/g;
var str = '123';
var myArray;
while (myArray = myRe.exec(str)) {
console.log(myArray);
}
链接地址: http://www.djcxy.com/p/76792.html
下一篇: 与捕获组的Javascript全球匹配