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 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短