- 문자열을 검색하고 대체하는 데 사용 가능한 일종의 형식 언어(패턴)입니다.
- 간단한 문자 검색부터 이메일 , 패스워드검사 등의 복잡한 문자 일치 기능 등을 정규식 패턴으로 빠르게 수행할 수 있습니다.
- 문자 검색
- 문자 대체
- 문자 추출
RegExp
생성자 함수를 호출하여 사용할 수 있습니다.
const regexp1 = new RegExp("abc");
// new RegExp(표현식)
const regexp2 = new RegExp("^abc","gi");
// new RegExp(표현식 , 플래그)
정규표현식은 /
로 감싸진 패턴을 리터럴로 사용합니다.
const regexp1 = /^abc/;
// /표현식/
const regexp2 = /^abc/gi;
// /표현식/플래그
gi
: 대소문자 구분 안함
메소드 | 문법 | 설명 |
---|---|---|
test | 정규식.test(문자열) |
일치 여부(Boolean) |
match | 문자열.match(정규식) |
일치하는 문자의 배열 반환 |
replace | 문자열.replace(정규식, 대체문자) |
일치하는 문자를 대체 |
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
`
const regexp = /fox/gi
console.log(regexp.test(str)) // true
console.log(str.replace(regexp,'AAA')) // fox >> AAA
플래그 | 설명 |
---|---|
g | 모든 문자 일치 |
i | 영어 대소문자를 구분 않고 일치 |
m | 여러 줄 일치 |
g , gi
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
`
const regexp = /the/
console.log(str.match(regexp)) // 소문자 the가 있는 단어 하나만 일치시킴
const regexp2 =/the/g
console.log(str.match(regexp)) // 소문자 the 전부 출력
const regexp3 = /the/gi
console.log(str.match(regexp)) // 대문자 The까지 전부 출력
m
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
`
console.log(str.match(/\.$/gim)) // ["."]
$
정규표현식에 일치하는 문자 첫번째 문장만 반환하도록 함.
\
(이스케이프 문자) 란 백슬래시 기호를 통해 본래의 기능에서 벗어나 상태가 바뀌는 문자를 말한다.
패턴 | 설명 |
---|---|
^ab | 줄 시작 에 있는 ab와 일치 |
ab$ | 줄 끝 에 있는 ab와 일치 |
. | 임의의 한 문자와 일치 |
a|b | a 또는 b와 일치 |
ab? | b가 없거나 b와 일치 |
{3} | 3개 연속 일치 |
{3 ,} | 3개 이상 연속 일치 |
{3 , 5} | 3개 이상 5개 이하(3~5개) 연속 일치 |
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
d
https://local.1234
http://local.12345`
console.log(
str.match(/d$/g)
) // ["d"]
console.log(
str.match(/d$/gm)
) // ["d"] ["d"]
console.log(
str.match(/^t/gim)
) // ["t"] , ["T"]
console.log(
str.match(/./g) // 모든 글자 배열이 나옴.
)
console.log(
str.match(/.a..e/g)
) // ["apple"]
console.log(
str.match(/fox|dog/g)
) //["fox"] , ["dog"] if g가 없으면 fox만 출력
console.log(
str.match(/https?/g)
) // ["https"] ,["http"]
console.log(
str.match(/d{2}/g)
) // dd dd << 배열임
console.log(
str.match(/\b\w{2,3}\b/g)
// 영어 숫자 구분없이 2개이상 3개 이하로 이루어진 단어들만 출력
)
패턴 | 설명 |
---|---|
[abc] | a 또는 b 또는 c |
[a-z] | a부터 z 사이의 문자 구간에 일치 (영어 소문자) |
[A-Z] | A부터 Z 사이의 문자 구간에 일치 (영어 대문자) |
[0-9] | 0부터 9 사이의 문자 구간에 일치(숫자) |
[가-힣] | 가부터 힣 사이의 문자 구간에 일치(한글) |
\w | 63개 문자(Word, 대소영문52개 + 숫자 10개 + _)에 일치 |
\b | 63개 문자에 일치하지 않는 문자 경계(Boundary) |
\d | 숫자(Digit)에 일치 |
\s | 공백에 일치 |
(?=) | 앞쪽 일치 |
(?<=) | 뒤쪽 일치 |
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
d
https://local.1234
http://local.12345`
const h = ` the hello world ! `
console.log(
str.match(/[fox]/g) // f랑 o 모두 찾아짐
)
console.log(
str.match(/[0-9]{1,}/g) //1개 이상의 0 ~ 9 모두 찾아짐
)
console.log(
str.match(/\w/g) // 영어 다 나옴
)
console.log(
str.match(/\bf\w{1,}/g) // f ~ 63개 문자가 아닌 경계에서 끊는다. ["fox"]
)
console.log(
h.replace(/\s/g,'') // 공백문자 삭제로 응용 가능
)
앞쪽일치 , 뒷쪽 일치
let str = `
010-1234-5678
s22043@gsm.hs.kr
The quick brown fox jumps over the lazy dog.
abbcccdddd
the apple
d
https://local.1234
http://local.12345`
console.log(
str.match(/.{1,}(?=@)/g) // 1개이상 @ 앞에 있는 배열 ["s22043"]
)
console.log(
str.match(/(?<=@).{1,}/g) // ["gmail.com"]
)