var patt=new RegExp(/\d+/g);
var str="dfgdfg5465yhhgh65y65hh41";
var result;
// var reset=patt.exec(str);
while ((result=patt.exec(str))!= null){
// alert(patt.lastIndex)
document.write(result);
document.write("<br />");
document.write(patt.lastIndex);
document.write("<br />");
}
document.write(str.match(patt));
document.write(patt.test(str));
exec 只匹配一次 返回数组格式 match如果有全局匹配模式g 一直匹配到底。test返回布尔值true和false.
所以要exec全部匹配完需要进行循环。每匹配一次正则对象会返回一个lastIndex属性,下一次在执行的时候就从lastindex开始向后面匹配。没有匹配则返回null;
match方法 字符串方法。返回数组。匹配到了是否接着匹配依靠全局g标志。