看下面这段代码: 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"); t...
1.lastIndex属性,返回匹配内容的最后一个索引位置,也就是下一个匹配的开始位置,注意字符串索引从0开始 代码: [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 2.lastMatch($&)属性,只读属性,...
前言 今天遇到一个问题,用正则表达式去检查同一个字符串时,交替返回true和false。无奈之下,重新翻了翻权威指南,发现罪魁祸首原来是lastIndex。可在控制台尝试下 let reg = /[\d]/g //undefined reg.test...
前言 今天遇到一个问题,用正则表达式去检查同一个字符串时,交替返回true和false。无奈之下,重新翻了翻权威指南,发现罪魁祸首原来是lastIndex。可在控制台尝试下 let reg = /[\d]/g //undefined reg.test(1) ...