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后的全局变量不是一个意思吗?
冠月明金
相关分类