var reg = /box/g; var str = 'this is a box , box '; while(reg.exec(str)) console.log(reg.lastIndex);
MDN: If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
lastIndex
指的是正则对象的属性,并不是exec
返回的结果的属性。MDN:
If the match succeeds, the exec() method returns an array and updates properties of the regular expression object. The returned array has the matched text as the first item, and then one item for each capturing parenthesis that matched containing the text that was captured.
你确定你正确理解这些话的意思了?
RegExp.exec返回的是结果数组. 不包含lastIndex这一类的东西. 只有截取的字符串.
lastIndex这一类的在RegExp对象内
详细请参阅MDN文档