javascript - 请教和正则的问题
怪我咯
怪我咯 2017-04-11 12:38:24
[JavaScript讨论组]

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

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



    
       
       正则表达式
       
               
    
    
      
    
怪我咯
怪我咯

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

全部回复(2)
PHP中文网

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.

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

PHP中文网

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

lastIndex这一类的在RegExp对象内

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

详细请参阅MDN文档

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号