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>热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短