<aside> π‘
μ κ· ννμ: λ¬Έμμ΄ λμμΌλ‘ ν¨ν΄ λ§€μΉ κΈ°λ₯ μ 곡
</aside>
/regexp/i
const regexp = /is/i;
// const regexp = new RegExp(/is/i)
const target = 'Is this all there is?';
const regExp = /is/;
regExp.exec(target); // -> ["is", index: 5, input: "Is this all there is?", groups: undefined]
λ§€μΉλ λ¬Έμμ΄, λ§€μΉ μμΉ μ 보
const target = 'Is this all there is?';
const regExp = /is/;
regExp.test(target); // -> true
λ§€μΉ λμλμ§ true, false λ°ν
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 ν¨μμ λμΌν κ²°κ³Ό
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' ]
| i | ignore case | λμλ¬Έμ κ΅¬λ³ x |
|---|---|---|
| g | global | μ¬λΏ κ²μ κ°λ₯ |
| m | multi line | λ¬Έμμ΄ ν λ°λλλΌλ κ²μ |
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β, β¦ κ²μ
console.log('A AA B BB Aa Bb AAA'.match(/[A-Z]+/g));
`
[
'A', 'AA',
'B', 'BB',
'A', 'B',
'AAA'
]
`
[]κ΄νΈμ -λ₯Ό μ¬μ©νμ¬ λ²μλ‘ κ²μ κ°λ₯
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_]μ λ°λλ₯Ό μλ―Έ
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/' ]
$λ λ§μ§λ§ λ¬Έμμ΄μ μλ―Έ
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
const target = 'Is this all there is?';
const regExp = /is/ig;
console.log(regExp.exec(target));
console.log(target.match(regExp));
console.log('A AA 12,3_45'.match(/[\\W]+/g));