RegExp

<aside> πŸ’‘

μ •κ·œ ν‘œν˜„μ‹: λ¬Έμžμ—΄ λŒ€μƒμœΌλ‘œ νŒ¨ν„΄ λ§€μΉ­ κΈ°λŠ₯ 제곡

</aside>

1. μ •κ·œ ν‘œν˜„μ‹ 생성

/regexp/i
  1. /: μ •κ·œμ‹ μ‹œμž‘
  2. regexp: νŒ¨ν„΄, νŒ¨ν„΄ λ¬Έμžμ—΄κ³Ό μΌμΉ˜ν•˜λŠ”μ§€ 검색
  3. /: μ •κ·œμ‹ 끝
  4. i: ν”Œλž˜κ·Έ, μ˜΅μ…˜ 제곡
const regexp = /is/i;
// const regexp = new RegExp(/is/i)
  1. λ¦¬ν„°λŸ΄ μƒμ„±λ²•μœΌλ‘œ 생성 κ°€λŠ₯
  2. new RegExp μƒμ„±μžλ₯Ό 톡해 생성 κ°€λŠ₯

2. RegExp λ§€μ„œλ“œ

RegExp.prototype.exec

const target = 'Is this all there is?';
const regExp = /is/;

regExp.exec(target); // -> ["is", index: 5, input: "Is this all there is?", groups: undefined]

맀칭된 λ¬Έμžμ—΄, λ§€μΉ­ μœ„μΉ˜ 정보

RegExp.prototype.test

const target = 'Is this all there is?';
const regExp = /is/;

regExp.test(target); // -> true

λ§€μΉ­ λ˜μ—ˆλŠ”μ§€ true, false λ°˜ν™˜

String.prototype.match

const target = 'Is this all there is?';
const regExp = /is/;

target.match(regExp); // -> ["is", index: 5, input: "Is this all there is?", groups: undefined]

exec ν•¨μˆ˜μ™€ λ™μΌν•œ κ²°κ³Ό

RegExp.prototype.exec vs String.prototype.match

const target = 'Is this all there is?';
const regExp = /is/g;

console.log(regExp.exec(target)); // [ 'is', index: 5, input: 'Is this all there is?', groups: undefined ]
console.log(target.match(regExp)); // [ 'is', 'is' ]
  1. match: RegExp ν”„λ‘œν† νƒ€μž… ν•¨μˆ˜, exec: String ν”„λ‘œν† νƒ€μž… ν•¨μˆ˜
  2. match: g μ˜΅μ…˜μœΌλ‘œ μ—¬λŸΏ 검색 κ°€λŠ₯, exec: μ•žμ„œ ν•˜λ‚˜λ§Œ 검색 κ°€λŠ₯

3. ν”Œλž˜κ·Έ

i ignore case λŒ€μ†Œλ¬Έμž ꡬ별 x
g global μ—¬λŸΏ 검색 κ°€λŠ₯
m multi line λ¬Έμžμ—΄ ν–‰ λ°”λ€Œλ”λΌλ„ 검색

4. νŒ¨ν„΄

λ¬Έμžμ—΄ 검색

console.log('Is this all there is?'.match(/is/ig)); // [ 'Is', 'is', 'is' ]

is와 κ²ΉμΉ˜λŠ” λ¬Έμžμ—΄ 검색

μž„μ˜μ˜ λ¬Έμžμ—΄ 검색

console.log('Is this all there is?'.match(/.../g));
console.log('Is this all there is?'.match(/../g));
`
[
  'Is ', 'thi',
  's a', 'll ',
  'the', 're ',
  'is?'
]
[
  'Is', ' t', 'hi',
  's ', 'al', 'l ',
  'th', 'er', 'e ',
  'is'
]
`

. ν•˜λ‚˜ λ‹Ή μž„μ˜μ˜ 문자 ν•œ 개λ₯Ό 의미

반볡 검색

console.log('A AA B BB Aa Bb AAA'.match(/A{1,2}/g)); // [ 'A', 'AA', 'A', 'AA', 'A' ]

{m,n}으둜 A의 반볡이 μ΅œμ†Œ 1번 μ΅œλŒ€ 2번 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄ 검색

console.log('A AA B BB Aa Bb AAA'.match(/A+/g)); // [ 'A', 'AA', 'A', 'AAA' ]

+기호λ₯Ό μ‚¬μš©ν•˜μ—¬ β€˜A’가 ν•œ 번 이상 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄ β€˜A’, β€˜AA’, β€˜AAA’, … 검색

or 검색

console.log('A AA B BB Aa Bb AAA'.match(/[A-Z]+/g));
`
[
  'A',   'AA',
  'B',   'BB',
  'A',   'B',
  'AAA'
]
`

[]κ΄„ν˜Έμ— -λ₯Ό μ‚¬μš©ν•˜μ—¬ λ²”μœ„λ‘œ 검색 κ°€λŠ₯

\d, \w

console.log('A AA 12,345'.match(/[\\d]+/g)); // [ '12', '345' ]
console.log('A AA 12,345'.match(/[0-9]+/g)); // [ '12', '345' ]

\dλ₯Ό μ‚¬μš©ν•˜μ—¬ [0-9] ν‘œν˜„ κ°€λŠ₯

console.log('A AA 12,345'.match(/[\\D]+/g)); // [ 'A AA ', ',' ]

\DλŠ” [0-9] μ΄μ™Έμ˜ 것을 의미

console.log('A AA 12,345'.match(/[\\w]+/g)); // [ 'A', 'AA', '12', '345' ]

\wλŠ” [A-Za-z0-9_]λ₯Ό 의미

console.log('A AA 12,345'.match(/[\\W]+/g)); // [ ' ', ' ', ',' ]

\WλŠ” [A-Za-z0-9_]의 λ°˜λŒ€λ₯Ό 의미

NOT 검색

console.log('A AA 12 Aa Bb'.match(/[^0-9]+/g)); // [ 'A AA ', ' Aa Bb' ]

[^…]: not을 의미, [0-9] μ œμ™Έν•˜κ³  검색과 λ™μΌν•˜λ‹€.

μ‹œμž‘ 및 끝 검색

console.log('<https://github.com/>'.match(/^https/g)); // ['https']

[]둜 λ‘˜λŸ¬μ‹Έμ—¬μžˆμ§€ μ•ŠμœΌλ©΄ ^λŠ” μ‹œμž‘μ„ μ˜λ―Έν•œλ‹€.

console.log('<https://github.com/>'.match(/com\\/$/g)); // [ 'com/' ]

$λŠ” λ§ˆμ§€λ§‰ λ¬Έμžμ—΄μ„ 의미

5. μ‹€μ œ μ‚¬μš© 예제

아이디 검사

console.log(/^[A-Za-z0-9]{4,10}$/g.test('abc123')); // true

μ˜μ–΄ λ¬Έμžμ™€ 숫자, 4~10κΈ€μžλ‘œ λ˜μ–΄ μžˆμ–΄μ•Όν•¨μ„ 검사

ν•Έλ“œν° 번호 ν˜•μ‹ 검사

console.log(/^\\d{2,3}-\\d{3,4}-\\d{4}$/g.test('010-1234-5678')); // true

Quiz

#1. λ‹€μŒμ˜ 좜λ ₯ κ²°κ³ΌλŠ”?

const target = 'Is this all there is?';
const regExp = /is/ig;

console.log(regExp.exec(target));
console.log(target.match(regExp));

#2. λ‹€μŒμ˜ 좜λ ₯ κ²°κ³ΌλŠ”?

console.log('A AA 12,3_45'.match(/[\\W]+/g));