반응형
문자열 메서드 : match()
match() 메서드는 문자열(정규식)을 검색하고 배열로 반환합니다.
"문자열".match("검색값"); "문자열".match(정규식 표현)
const str1 = "javascript reference";
const currentStr1 = str1.match("javascript"); //javascript
const currentStr2 = str1.match("reference"); //reference
const currentStr3 = str1.match("r"); //r
const currentStr4 = str1.match(/reference/); //reference
const currentStr5 = str1.match(/Reference/); //null
const currentStr6 = str1.match(/Reference/i); //reference
const currentStr7 = str1.match(/r/g); //['r', 'r', 'r']
const currentStr8 = str1.match(/e/g); //['e', 'e', 'e', 'e']
match() 예제
var test = 'love you. love me. love everything!'
var regExp = /love/gi;
test2 = test.match(regExp); //['love', 'love', 'love']
정규 표현식을 사용하여 텍스트에 Love가 있는지 확인합니다.
정규표현식 코드는 따로 regExp 변수에 저장하였으며, 표현식 뒤에 gi는 대소문자 구분을 허용하지 않고 모든 패턴을 검색하기 위함입니다.
반응형
'Javascript' 카테고리의 다른 글
GSAP (9) | 2022.08.29 |
---|---|
charAt() (10) | 2022.08.22 |
search() (6) | 2022.08.22 |
함수 유형 (4) | 2022.08.22 |
includes() (7) | 2022.08.17 |
댓글