html5中文学习网

您的位置: 首页 > 网络编程 > 正则表达式 » 正文

Javascript lastIndex 正则表达式的一个疑惑_正则表达式_脚本之家

[ ] 已经帮助:人解决问题
看下面这段代码:
function test(s){
var reg = /./g;
console.log(reg.exec(s));
console.log(reg.lastIndex);
var reg = /./g;
console.log(reg.exec(s));
console.log(reg.lastIndex);
}
test("abcd");
test("efgh");
我以为输出的lastIndex的值应该都是1,但是实际上的输出如下:
a
1
a
1
f
2
f
2
感觉就像是在第二次调用test的时候第2行和第6行并没有产生新的正则表达式,其之前的属性lastIndex还保留着(lastIndex=1)。这有点不合常理,头疼中。。。。。。
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助