Javascript RegExp.lastIndex属性
作者:--
发布时间:2019-11-20
评论:0
阅读:1
lastindex的是正则表达式的对象的读/写属性。对于用“g”属性设置正则表达式,它包含一个整数,指定的字符位置紧接在最后被regexp.exec()和regexp.test()方法找到匹配。这些方法使用该属性为出发点,在以后进行搜索。
这个属性反复调用这些方法,遍历字符串中的所有匹配和只能当“g”修改被设置时。
此属性为读/写,这样就可以在任何时间以指定目标字符串在未来的搜索应该开始设置。执行exec()和test()自动复位lastindex为0时,他们无法找到匹配(或其它匹配项)。
语法
regexpobject.lastindex
下面是参数的详细信息:
返回值:
返回一个整数,指定字符位置最后一次匹配之后。
例子:
<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