์ ๊ท ํํ์์ ์ผ์ ํ ํจํด์ ๊ฐ์ง ๋ฌธ์์ด์ ์งํฉ์ ํํํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ํ์ ์ธ์ด
๋ค. ์ ๊ท ํํ์์ ๋ฌธ์์ด์ ๋์์ผ๋ก ํจํด ๋งค์นญ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค. ํจํด ๋งค์นญ ๊ธฐ๋ฅ์ด๋ ํน์ ํจํด๊ณผ ์ผ์นํ๋ ๋ฌธ์์ด์ ๊ฒ์ํ๊ฑฐ๋ ์ถ์ถ ๋๋ ์นํํ ์ ์๋ ๊ธฐ๋ฅ์ ๋งํ๋ค
์ ๊ท ํํ์์ ์ฃผ์์ด๋ ๊ณต๋ฐฑ์ ํ์ฉํ์ง ์๊ณ ์ฌ๋ฌ๊ฐ์ง ๊ธฐํธ๋ฅผ ํผํฉํ์ฌ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๊ฐ๋
์ฑ์ด ์ข์ง ์๋ค..`
์ ๊ท ํํ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ์ํด์๋ ์ ๊ท ํํ์ ๋ฆฌํฐ๋ด๊ณผ RegExp ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ ์ ๊ท ํํ์ ๋ฆฌํฐ๋ด์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
const target = 'Is this all there is?';
// ํจํด: is
// ํ๋๊ทธ: i => ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ง ์๊ณ ๊ฒ์ํ๋ค
const regexp = /is/i;
// test ๋ฉ์๋๋ target ๋ฌธ์์ด์ ๋ํด ์ ๊ท ํํ์ regexp์ ํจํด์ ๊ฒ์ํ์ฌ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ
// ๋ถ๋ฆฌ์ธ ๊ฐ์ผ๋ก ๋ฐํํ๋ค.
regexp.text(target); // true
RegExp ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ RegExp ๊ฐ์ฒด๋ฅผ ์์ฑํ ์๋ ์๋ค.
const target = 'Is this all there is?';
const regexp = new RegExp(/is/i); // ES6
// const regexp = new RegExp(/is/, 'i');
// const regexp = new RegExp('is', 'i);
regexp.text(target); // true
RegExp ์์ฑ์ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ณ์๋ฅผ ์ฌ์ฉํด ๋์ ์ผ๋ก RegExp ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค.
const count = (str, char) => (str.match(new RegExp(char, 'gi')) ?? []).length;
count('Is this all there is?'. 'is'); // 3
count('Is this all there is?', 'xx'); // 0
exec ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ฌธ์์ด์ ๋ํด ์ ๊ท ํํ์์ ํจํด์ ๊ฒ์ํ์ฌ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ. ๋งค์นญ ๊ฒฐ๊ณผ๊ฐ ์๋๊ฒฝ์ฐ null
const target = 'Is this all there is?;
const regExp = /is/;
regExp.exec(target);
// ["is", index: 5, input : "Is this all there is?", groups: undefined]
exec ๋ฉ์๋๋ ๋ฌธ์์ด ๋ด์ ๋ชจ๋ ํจํด์ ๊ฒ์ํ๋ g ํ๋๊ทธ๋ฅผ ์ง์ ํด๋ ์ฒซ ๋ฒ์จฐ ๋งค์นญ ๊ฒฐ๊ณผ๋ง ๋ฐํํ๋ฏ๋ก ์ฃผ์
test ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ฌธ์์ด์ ๋ํด ์ ๊ท ํํ์์ ํจํด์ ๊ฒ์ํ์ฌ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ถ๋ฆฌ์ธ ๊ฐ์ผ๋ก ๋ฐํ
const target = 'Is this all there is?';
const regExp = /is/;
regExp,text(target); // true
String ํ์ค ๋นํธ์ธ ๊ฐ์ฒด๊ฐ ์ ๊ณตํ๋ match ๋ฉ์๋๋ ๋์ ๋ฌธ์์ด๊ณผ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ์ ๊ท ํํ์๊ณผ์ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ.
const traget = 'Is this all there is?';
const regExp = /is/;
target.match(regExp);
// ["is", index: 5, input: "Is this all there is?", groups: undefined]
match. ๋ฉ์๋๋ g ํ๋๊ทธ๊ฐ ์ง์ ๋๋ฉด ๋ชจ๋ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ
const target = 'Is this all there is?';
const regExp = /is/g;
target.match(regExp); // ["is", "is"]
ํจํด๊ณผ ํจ๊ป ์ ๊ท ํํ์์ ๊ตฌ์ฑํ๋ ํ๋๊ทธ๋ ์ ๊ท ํํ์์ ๊ฒ์ ๋ฐฉ์์ ์ค์ ํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค ํ๋๊ทธ๋ 6๊ฐ์ง๋ง ์ค์ ํ 3๊ฐ์ ํ๋๊ทธ๋ฅผ ์์๋ณด์
ํ๋๊ทธ | ์๋ฏธ | ์ค๋ช |
---|---|---|
i | Ignore case | ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ง ์๊ณ ํจํด์ ๊ฒ์ํ๋ค. |
g | Global | ๋์ ๋ฌธ์์ด ๋ด์์ ํจํด๊ณผ ์ผ์นํ๋ ๋ชจ๋ ๋ฌธ์์ด์ ์ ์ญ ๊ฒ์ํ๋ค. |
m | Multi line | ๋ฌธ์์ด์ ํ์ด ๋ฐ๋๋๋ผ๋ ํจํด ๊ฒ์์ ๊ณ์ํ๋ค. |
์ ๊ท ํํ์์ ์ผ์ ํ ๊ท์น์ ๊ฐ์ง ๋ฌธ์์ด์ ์งํฉ์ ํํํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ํ์ ์ธ์ด๋ค. ์ ๊ท ํํ์์ ํจํด๊ณผ ํ๋๊ทธ๋ก ๊ตฌ์ฑ
์ ๊ท ํํ์์ ํจํด์ ๋ฌธ์ ๋๋ ๋ฌธ์์ด์ ์ง์ ํ๋ฉด ๊ฒ์ ๋์ ๋ฌธ์์ด์์ ํจํด์ผ๋ก ์ง์ ํ ๋ฌธ์ ๋๋ ๋ฌธ์์ด์ ๊ฒ์ํ๋ค. RegExp ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์ ๋์ ๋ฌธ์์ด๊ณผ ์ ๊ท ํํ์์ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ๋ฉด ๊ฒ์์ด ์ํ๋๋ค.
const target = 'Is this all there is?';
// 'is' ๋ฌธ์์ด๊ณผ ๋งค์นํ๋ ํจํด, ํ๋๊ทธ๊ฐ ์๋ต๋์์ผ๋ฏ๋ก ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ๋ค.
const regExp = /is/;
// target๊ณผ ์ ๊ท ํํ์์ด ๋งค์นํ๋์ง ํ
์คํธํ๋ค.
regExp.test(target); // true
// target๊ณผ ์ ๊ท ํํ์์ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ๋ค.
target.matach(regExp);
// ["is", index: 5, input: "Is this all there is", groups: undefined]
๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ง ์๊ณ ๊ฒ์ํ๋ ค๋ฉด ํ๋๊ทธ i๋ฅผ ์ฌ์ฉ
๊ฒ์ ๋์ ๋ฌธ์์ด ๋ด์์ ํจํด๊ณผ ๋งค์นํ๋ ๋ชจ๋ ๋ฌธ์์ด์ ์ ์ญ ๊ฒ์ํ๋ ค๋ฉด ํ๋๊ทธ g๋ฅผ
.์ ์์์ ๋ฌธ์ ํ ๊ฐ๋ฅผ ์๋ฏธํ๋ค. ๋ฌธ์์ ๋ด์ฉ์ ๋ฌด์์ด๋ ์๊ด์๋ค.
const target = 'Is this all there is?';
// ์์์ 3์๋ฆฌ ๋ฌธ์์ด์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ฌ ์ ์ญ ๊ฒ์ํ๋ค.
const regExp = /.../g;
target.match(rgeExp); // ["Is", "thi", "s a", "li", "the", "re", "is?"]
{m,n}์ ์์ ํจํด์ด ์ต์ m๋ฒ, ์ต๋ n๋ฒ ๋ฐ๋ณต๋๋ ๋ฌธ์์ด์ ์๋ฏธํ๋ค.
์ฝค๋ง ๋ค์ ๊ณต๋ฐฑ์ด ์์ผ๋ฉด ์ ์ ๋์ํ์ง ์์ผ
๋ฏ๋ก ์ฃผ์
const target = 'A AA BB Aa Bb AAA';
// 'A'๊ฐ ์ต์ 1๋ฒ, ์ต๋ 2๋ฒ ๋ฐ๋ณต๋๋ ๋ฌธ์์ด์ ์ ์ญ ๊ฒ์ํ๋ค.
const regExp = /A{1,2}/g;
target.match(regExp); // ["A", "AA", "A", "AA", "A"]
|์ or์ ์๋ฏธ๋ฅผ ๊ฐ๋๋ค. ๋ค์ ์์ ์ /A|B/๋ 'A' ๋๋ 'B'๋ฅผ ์๋ฏธํ๋ค.
const target = 'A AA B BB Aa Bb';
// 'A' ๋๋ 'B'๋ฅผ ์ ์ญ ๊ฒ์ํ๋ค
const regExp = /A|B/g;
target.match(regExp); ["A", "A", "A", "B", "B", "B", "A", "B"]
๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ง ์๊ณ ์ํ๋ฒณ์ ๊ฒ์ํ๋ ๋ฐฉ๋ฒ
const target = 'AA BB Aa Bb 12';
// 'A' ~ 'Z' ๋๋ 'a' ~ 'z'๊ฐ ํ ๋ฒ ์ด์ ๋ฐ๋ณต๋๋ ๋ฌธ์์ด์ ์ ์ญ ๊ฒ์ํ๋ค.
const regExp = /[A-Za=z]+/g;
target.match(regExp); // ["AA", "BB", "Aa", "Bb"]
์ซ์๋ฅผ ๊ฒ์ํ๋ ๋ฐฉ๋ฒ
const target = 'AA BB 12,345';
// 'a' ~ '9'๊ฐ ํ ๋ฒ ์ด์ ๋ฐ๋ณต๋๋ ๋ฌธ์์ด์ ์ ์ญ ๊ฒ์ํ๋ค.
const regExp = /[0-9]+/g;
target.match(regExp); // ["12", "345"]
[...] ๋ด์ ^์ not์ ์๋ฏธ๋ฅผ ๊ฐ๋๋ค.
const target = 'AA BB 12 Aa Bb';
// ์ซ์๋ฅผ ์ ์ธํ ๋ฌธ์์ด์ ์ ์ญ ๊ฒ์ํ๋ค.
const regExp = /[^0-9]+/g;
target.match(regExp); // ["AA BB", "Aa Bb"]
[...] ๋ฐ์ ^์ ๋ฌธ์์ด์ ์์์ ์๋ฏธํ๋ค. ๋จ, [...] ๋ด์ ^์ not์ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ฏ๋ก ์ฃผ์ํ๊ธฐ ๋ฐ๋๋ค.
const target = 'https://poiemaweb.com';
// 'https'๋ก ์์ํ๋์ง ๊ฒ์ฌํ๋ค.
const regExp = /^https/;
regExp.text(target); // true
$์ ๋ฌธ์์ด์ ๋ง์ง๋ง์ ์๋ฏธํ๋ค
const target = 'https://poiemaweb.com';
// 'com'๋ก ์์ํ๋์ง ๊ฒ์ฌํ๋ค.
const regExp = /com$/;
regExp.text(target); // true
[...] ๋ฐ๊นฅ์ ^์ ๋ฌธ์์ด์ ์์์ ์๋ฏธํ๊ณ , ?์ ์์ ํจํด์ด ์ต๋ ํ๋ฒ์ด์ ๋ฐ๋ณต๋๋์ง ์๋ฏธํ๋ค. ๋ค์ ๋งํด,
๊ฒ์ ๋์ ๋ฌธ์์ด์ ์์ ํจํด('s')์ด ์์ด๋ ์์ด๋ ๋งค์น๋๋ค.
๊ฒ์ ๋์ ๋ฌธ์์ด์ด 'html'๋ก ๋๋๋์ง ๊ฒ์ฌํ๋ค.
$
ใ กใด ๋ฌธ์์ด์ ๋ง์ง๋ง์ ์๋ฏธ
const fileName = 'index.html';
// 'html'๋ก ๋๋๋์ง ๊ฒ์ฌํ๋ค.
/html$/.test(fileName); // true
[...] ๋ฐ๊นฅ์ ^์ ๋ฌธ์์ด์ ์์์. $๋ ๋ฌธ์์ด์ ๋ง์ง๋ง์ ์๋ฏธํ๋ค. \d๋ ์ซ์๋ฅผ ์๋ฏธํ๊ณ +๋ ์์ ํจํด์ด ์ต์ ํ ๋ฒ ์ด์ ๋ฐ๋ณต๋๋ ๋ฌธ์์ด์ ์๋ฏธ. ์ฆ
์ฒ์๊ณผ ๋์ด ์ซ์์ด๊ณ ์ต์ ํ ๋ฒ ์ด์ ๋ฐ๋ณต๋๋ ๋ฌธ์์ด๊ณผ ๋งค์น
const fileName = '12345';
// ์ซ์๋ก๋ง ์ด๋ฃจ์ด์ง ๋ฌธ์์ด์ธ์ง ๊ฒ์ฌํ๋ค.
/^\d+$/.test(target); // true
const target = 'Hi!';
// ํ๋ ์ด์์ ๊ณต๋ฐฑ์ผ๋ก ์์ํ๋์ง ๊ฒ์ฌ
/^[\s]+/.test(target); // true
const id = 'abc123';
// ์ํ๋ฒณ ๋์๋ฌธ์ ๋๋ ์ซ์๋ก ์์ํ๊ณ ๋๋๋ฉฐ 4~10์๋ฆฌ์ธ์ง ๊ฒ์ฌํ๋ค.
/^[A-Za-z0-9]{4,10}$/.text(id); // true
const email = 'ungmo2@gmail.com';
/^[0-9a-zA-Z]([-_\.]?[0-9a-zA-z])*@[0-9a-zA-z]([-_\.]?[0-9a-zA-Z])*\.[a=zA-Z]{2,3}$/.text(email); // true
const cellphone = '010-1234-5678';
/^\d{3}-\d{3,4}-\d{4}$/.test(cellphone); // true
const target = 'abc#123';
(/[^A-Za-z0-9]).text(target); // true
ํน์๋ฌธ์ ๋ฅผ ์ ๊ฑฐํ ๋๋ String.prototype.replace ๋ฉ์๋๋ฅผ ์ฌ์ฉ