자바스크립트 lastIndex 속성
예시
let text = "The rain in Spain stays mainly in the plain";
let pattern = /ain/g;
let result = "";
while (pattern.test(text)==true) {
result += "Found at pos " + pattern.lastIndex + "<br>";
}
정의 및 사용
lastIndex 속성은 다음 일치를 시작할 인덱스를 지정합니다.
참고: 이 속성은 "g" 수정자가 설정된 경우에만 작동합니다.
이 속성은 exec( ) 또는 test( ) 메서드에서 찾은 마지막 일치 직후의 문자 위치를 지정하는 정수를 반환합니다.
참고: exec( ) 및 test( )는 일치하지 않는 경우 lastIndex를 0으로 재설정합니다.
브라우저 지원
lastIndex
ECMAScript1(ES1) 기능입니다.
ES1(JavaScript 1997)은 모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
통사론
RegExpObject.lastIndex
반환 값
Type | Description |
---|---|
Number | An integer that specifies the character position immediately after the last match found by exec( ) or test( ) methods |