javascript - 请教和正则的问题
怪我咯
怪我咯 2017-04-11 12:38:24
0
2
257

首先,javascript中正则的exec方法返回的既然是数组为什么下面代码的length值为1而不是2,

还有就是exec的lastindex属性为什么是undefined???

    正则表达式  
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all (2)
Ty80

lastIndex指的是正则对象的属性,并不是exec返回的结果的属性。

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.

你确定你正确理解这些话的意思了?

    Ty80

    RegExp.exec返回的是结果数组. 不包含lastIndex这一类的东西. 只有截取的字符串.

    lastIndex这一类的在RegExp对象内

    var re = /hi/ re.exec('hi') re.lastIndex // => 0

    详细请参阅MDN文档

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!