JavaScript 문자열 일치()
예
문자열을 사용하여 "ain" 검색:
let text = "The rain in SPAIN stays mainly in the plain";
text.match("ain");
정규 표현식을 사용한 "ain" 검색:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/);
"ain"에 대한 글로벌 검색:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
대소문자를 구분하지 않는 전역 검색:
let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
정의 및 사용
이 match()
메서드는 문자열을 정규식과 일치시킵니다 **
이 match()
메서드는 일치 항목이 있는 배열을 반환합니다.
일치하는 항목 이 없으면 match()
메서드가 null 을 반환합니다.
통사론
string.match(match)
매개변수
Parameter | Description |
match | Required. The search value. A regular expression (or a string that will be converted to a regular expression). |
반환 값
유형 | 설명 |
배열 또는 null | 일치 항목이 포함된 배열입니다. null 일치하는 항목이 없는 경우. |
문자열 match()와 문자열 search() 의 차이점
이 match()
메서드는 일치하는 배열을 반환합니다.
이 search()
메서드는 첫 번째 일치 항목의 위치를 반환합니다.
정규식 검색 방법
JavaScript에서 정규식 텍스트 검색은 다른 방법으로 수행할 수 있습니다.
패턴 을 정규식으로 사용하는 경우 가장 일반적인 방법은 다음과 같습니다.
예시 | 설명 |
---|---|
text.match( 패턴 ) | 문자열 메서드 match() |
text.search( 패턴 ) | 문자열 메서드 search() |
패턴 .exec(텍스트) | RexExp 메소드 exec() |
패턴 .test(텍스트) | RegExp 메서드 test() |
브라우저 지원
match()
ECMAScript1(ES1) 기능입니다.
ES1(JavaScript 1997)은 모든 브라우저에서 완벽하게 지원됩니다.
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |