• 회원가입
  • 로그인
  • 구글아이디로 로그인

[RegExp] JS - lastIndex 속성 - 일치 직후의 문자 위치 반환 (= lastIndex속성 = 라스트인덱스 속성)

목차
  1. lastIndex 예제 - 일치 여부 체크할 다음 시작점
  2. lastIndex 정의
  3. lastIndex 구문

 

lastIndex 예제 - 일치 여부 체크할 다음 시작점

 

<p id="demo"></p>


<script>

let text = "Welcome to Homzzang.com !!";

let pattern = /om/g;


let result = "";

while (pattern.test(text)==true)  {

  result += "다음 일치 시작점: " + pattern.lastIndex + "<br>";

}


document.getElementById("demo").innerHTML = result;

</script>

 

결과보기


PS. 결과값

다음 일치 시작점: 6

다음 일치 시작점: 14

다음 일치 시작점: 23

 

 

lastIndex 정의

 

일치 여부 체크할 다음 시작 인덱스(=색인번호) 정수 반환.

 


 

1.

  • g 수정자 설정된 경우에만 작동.
  • exec() / test() 메서드에서 찾은 마지막 일치 직후의 문자 위치를 지정하는 정수를 반환.
  • exec() / test() 메서드는 일치 않을 시 lastIndex를 0으로 재설정.

 

2.

  • ECMAScript1(ES1 : JavaScript 1997)
  • 모든 브라우저 지원.

 

 

lastIndex 구문

 

RegExpObject.lastIndex

 


[반환값]

 

exec() / test() 메서드에서 찾은 마지막 일치 직후의 문자 위치를 지정하는 정수 반환.

 



분류 제목
DOM_Attribute JS - attr.hasAttributes() 메서드 -
DOM_Attribute JS - attr.hasChildNodes -
DOM_Attribute JS - attr.insertBefore() 메서드 -
DOM_Attribute JS - attr.isEqualNode() 메서드 -
DOM_Attribute JS - attr.isSameNode() 메서드 -
DOM_Attribute JS - attr.isSupported() 메서드 -
DOM_Attribute JS - attr.lastChild -
DOM_Attribute JS - attr.nextSibling -
DOM_Attribute JS - attr.nodeName - 사용금지 (※ 대신, attr.name 사용)
DOM_Element JS - attr.nodeType -
DOM_Attribute JS - attr.nodeValue -
DOM_Attribute JS - attr.normalize() 메서드 - 사용금지
DOM_Attribute JS - attr.ownerDocument -
DOM_Attribute JS - attr.ownerElement -
DOM_Attribute JS - attr.parentNode -
DOM_Attribute JS - attr.previousSibling -
DOM_Attribute JS - attr.removeChild -
DOM_Attribute JS - attr.replaceChild -
DOM_Attribute JS - attr.textContent -
Basic JS - const (컨스트 = 콘스트) ★★★★★ - 재할당 불가한 변수선언
21/67
목록
찾아주셔서 감사합니다. Since 2012