lastIndex RegExp 设置了“g”属性的正则表达式,它包含一个整数,该整数指定紧随RegExp.exec()()和RegExp.test()()方法找到的最后一个匹配项之后的字符位置。 这些方法使用此属性作为它们进行下一次搜索的起点,exec()和test()在找不到匹配时自动将lastIndex重置为0。
RegExpObject.lastIndex
返回一个整数,它指定紧跟在最后一个匹配项之后的字符位置。
链接:https://www.learnfk.comhttps://www.learnfk.com/javascript/regexp-lastindex.html
来源:LearnFk无涯教程网
<html> <head> <title>JavaScript RegExp lastIndex Property</title> </head> <body> <script type="text/javascript"> var str="Javascript is an interesting scripting language"; var re=new RegExp( "script", "g" ); re.test(str); document.write("Test 1 - Current Index: " + re.lastIndex); re.test(str); document.write("<br />Test 2 - Current Index: " + re.lastIndex); </script> </body> </html>
运行上面代码输出
Test 1 - Current Index: 10 Test 2 - Current Index: 35
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)