문자열을 찾아 배열 객체로 가져오는 방법 - .match()

match는 정규표현식에 맞는 문자열을 찾아서 배열 객체로 반환합니다.

.match()

.match()는 정규표현식에 맞는 문자열을 찾아서 배열 객체로 반환합니다.

 

기본형

string.match( regexp );
  • 만약 정규표현식에 맞는 문자열이 없다면 null을 반환합니다.

 

예제

Lorem Ipsum Dolor 문자열에서 Lo가 있는지, Lo를 포함한 단어가 있는지, Loi가 있는지 검색하고, 그 결과를 출력하는 예제입니다.

<p id="ab">Lorem Ipsum Dolor</p>
<hr>

<p><strong>RegExp - /Lo/</strong></p>
<script>
  var Str = document.getElementById( 'ab' ).innerHTML;
  var Match = Str.match( /Lo/ );
  document.write( Match );
</script>

<p><strong>RegExp - /Lo\w+/</strong></p>
<script>
  var Str = document.getElementById( 'ab' ).innerHTML;
  var Match = Str.match( /Lo\w+/ );
  document.write( Match );
</script>

<p><strong>RegExp - /Loi/</strong></p>
<script>
  var Str = document.getElementById( 'ab' ).innerHTML;
  var Match = Str.match( /Loi/ );
  document.write( Match );
</script>

로딩 중... 잠시만 기다려주세요.
자바스크립트를 허용해주세요.

같이 보면 좋은 글

Post a Comment