less than 1 minute read

String.match().length란?

String.match().length를 알고자 한다면 먼저 String.match()가 무엇인지 이해해야 한다.

String.match()란?
String.match()는 String methods중 하나로 정규식을 이용해 값에 일치하는 문자(열)을 배열로 반환한다.
일치하는 값이 없으면 null을 반환한다.

위의 String.match()를 이해했다면 String.match().length가 무슨 일을 하는지 짐작이 갈 것이다.

그렇다. String.match().length는 String.match()에서 반환된 배열의 개수를 알려준다.

아래의 예시를 통해 알아보자.

var para = 'This is Jihyun\'s study Log. Nice to meet you.';

console.log(para.match(/jihyun/g)); //returns null
//console.log(para.match(/jihyun/g).length); //returns TypeError: Cannot read property 'length' of null

console.log(para.match(/[A-Z]/g)); //returns ["T", "J", "L", "N"]
console.log(para.match(/[A-Z]/g).length); //returns 4


var para2 = 'AaaaBbbb'

console.log(para2.match(/[A-Z]/gi)); //returns ["A", "a", "a", "a", "B", "b", "b", "b"]
console.log(para2.match(/[A-Z]/gi).length); //returns 8

참고: 정규식에 g(전역검색)와 i(대소문자 무시)는 패턴변경자이다. 더 궁금하다면?

Leave a comment