为了账号安全,请及时绑定邮箱和手机立即绑定

帮忙解释下下列代码

帮忙解释下下列代码

哎呀_哈哈 2016-09-26 19:11:36
var text = "cat,bat,sat,fat";   //数组  var pattern1 = /.at/;  var matches = pattern1.exec(text);  //局部变量  alert(matches.index);     //0  alert(matches[0]);     //cat  alert(pattern1.lastIndex);   //0  matches = pattern1.exec(text);  //全局变量  alert(matches.index);  alert(matches[0]);  alert(pattern1.lastIndex);  var pattern2 = /.at/g;    //全局变量  var matches = pattern2.exec(text);  alert(matches.index);    //0  alert(matches[0]);     //cat  alert(pattern2.lastIndex);   //3  matches = pattern2.exec(text);  alert(matches.index);         //4  alert(matches[0]);   //bat  alert(pattern2.lastIndex);  //7为什么最后一个alert弹出7呢?pattern1后的全局变量与pattern2后的全局变量不是一个意思吗?
查看完整描述

1 回答

?
冠月明金

TA贡献1条经验 获得超0个赞

pattern1不是全局匹配。

pattern2是全局匹配pattern2.lastIndex表示下一次匹配开始的下标。

执行了两次pattern2.exec(text)下一次开始的下标是7.

RegExp.prototype.exec()



查看完整回答
反对 回复 2016-10-12
  • 1 回答
  • 0 关注
  • 1293 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信