lastIndex RegExp 속성을 사용하면 해당 메서드를 반복적으로 호출하고 문자열의 모든 일치 항목을 반복할 수 있으며 "g" 수정자가 설정된 경우에만 작동합니다.
이 속성은 읽기/쓰기이므로 대상 문자열에서 다음 검색을 시작해야 하는 위치를 지정하기 위해 언제든지 설정할 수 있습니다. exec() 및 test()는 일치하는 항목(또는 다른 일치 항목)을 찾지 못하면 lastIndex를 자동으로 0으로 재설정합니다.
예시
다음 코드를 실행하여 JavaScript의 lastIndex RegExp 속성으로 작업할 수 있습니다. −
<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>