JavaScript中lastIndex RegExp属性的作用是什么?
lastIndexRegExp属性使您可以重复调用这些方法,以循环遍历字符串中的所有匹配项,并且仅在设置了“g”修饰符的情况下才起作用。
该属性是可读写的,因此您可以随时对其进行设置,以指定下一个搜索应在目标字符串中的何处开始。exec()并test()在找不到匹配项(或另一个匹配项)时自动将lastIndex重置为0。
示例
您可以尝试运行以下代码以使用JavaScript中的lastIndexRegExp属性-
<html>
<head>
<title>JavaScript RegExp lastIndex Property</title>
</head>
<body>
<script>
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>热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短