μ κ·ννμμ λ¬Έμμ΄μμ νΉμ μ‘°ν©μ μΆμΆνκΈ° μν ν¨ν΄μ΄λ€.
μ κ·ννμλ κ°μ²΄λ‘ μ·¨κΈλλ©°, μ£Όλ‘ κ°μ²΄ λ΄μ₯ λ©μλ λλ λ¬Έμμ΄ λ©μλμ ν¨κ» μ°μΈλ€.
const myReg = new RegExp("pattern", "flags");
const myLitReg = /pattern/flags;
리ν°λ΄ ννμμ ν¨ν΄μ΄ λ¬Έμμ΄ νμμΌλ‘ λ€μ΄κ°μ§ μλλ€. κ·Έλ κΈ° λλ¬Έμ ν νλ¦Ώ 리ν°λ΄μ μ΄μ©ν λμ μΈ μμ±μ΄ λΆκ°λ₯νλ€.
ν¨ν΄μ΄ νμ μ μΌ λ 리ν°λ΄ ννμμ μ¬μ©νλ©΄ μ’λ€.
λ¬Έμ κ²μ, λ¬Έμ μΆμΆ, λ¬Έμ λ체 λ± λ€μνκ² μ°μΌ μ μκΈ° λλ¬Έμ μ κ·μμ μ΅μν΄μ§λ κ² μ’λ€.
const str = `Hi there, Nice to meet you. And Hello there and hi.
I love grey(gray) color not a gry, graay and graaay.
Ya ya YaYaYa Ya
`;
const regExp = /Hi/;
console.log(str.match(regExp));
[
'Hi',
index: 0,
input: 'Hi there, Nice to meet you. And Hello there and hi.\n' +
'I love grey(gray) color not a gry, graay and graaay.\n' +
'Ya ya YaYaYa Ya\n',
groups: undefined
]
ν¨ν΄μ λ§ κ·Έλλ‘ μ°ΎμΌλ €λ λ¬Έμ ν¨ν΄μ΄λ€. λ¬Έμκ° κ·Έλλ‘ μλ λκ³ νμ ν λ¬Έμ νλκ·Έμ ννλ‘ μ¬ μλ μλ€.
const str = `Hi there, Nice to meet you. And Hello there and hi.
I love grey(gray) color not a gry, graay and graaay.
Ya ya YaYaYa Ya
`;
const regExp = /Hi/;
const newRegExp = /Hi/gi;
console.log(str.match(regExp));
console.log(str.match(newRegExp));
[
'Hi',
index: 0,
input: 'Hi there, Nice to meet you. And Hello there and hi.\n' +
'I love grey(gray) color not a gry, graay and graaay.\n' +
'Ya ya YaYaYa Ya\n',
groups: undefined
]
[ 'Hi', 'hi' ]
νλκ·Έλ μ μμμ newRegExpμ²λΌ 리ν°λ΄μΌ κ²½μ° μ κ·μμ λ€μ λΆμΈλ€. μ£Όλ‘ μ¬μ©λλ νλκ·Έλ λ€μκ³Ό κ°λ€.
const str = `Hello there, Nice to meet you. And Hello there and hi. Bye~
Hello I love grey(gray) color not a gry Bye~, Hello graay and graaay. Bye~
Hello Ya ya Hello YaYaYa Bye~ Ya Bye~
`;
const shortStr0 = `Hello my name is Lee.`;
const shortStr1 = `Hello my name is Lee. See u later`;
const regExp0 = /^Hello/g;
const regExp1 = /^Hello/gm;
const regExp2 = /Bye~$/gm;
const regExp3 = /^Hello my name is Lee.$/;
console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
console.log(shortStr0.match(regExp3));
console.log(shortStr1.match(regExp3));
[ 'Hello' ]
[ 'Hello', 'Hello', 'Hello' ]
[ 'Bye~', 'Bye~', 'Bye~' ]
[
'Hello my name is Lee.',
index: 0,
input: 'Hello my name is Lee.',
groups: undefined
]
null
μ£Όμ΄μ§ μ κ·μμμ ^ λ¬Έμλ ν μ€νΈμ μμ μμΉμ λμνλ€. κ·Έλ κΈ° λλ¬Έμ strμ Hello μ€μμλ λ¬Έμμ μμ λΆλΆμλ§ λ§€μΉλλ€.
μ΄λ regExp1μ κ²½μ° m μ΅μ μ μ¬μ©νλλ°, μ€ λ¨μλ‘ νμνκΈ° λλ¬Έμ Helloκ° 3κ° λ§€μΉλλ€.
$ λ¬Έμλ λ¬Έμ₯μ λκ³Ό λμλλ€. regExp2μ μ€ν κ²°κ³Όλ₯Ό 보면 m μ΅μ μ λ°λΌ μ€ λ¨μλ‘ λ λΆλΆμ Bye~λ§ λ°ννλ κ²μ λ³Ό μ μλ€.
^μ $μ ν¨κ» μ¬μ©νλ©΄ λ¬Έμ₯μ΄ μ κ·μκ³Ό μμ ν μΌμΉν κ²½μ°λ§ 맀μΉλ κ²μΌλ‘ κ°μ£Όνλ€.
shortStr0κ³Ό shortStr1μμ regExp3μ λ°ν κ²°κ³Όλ₯Ό 보면 κ·Έκ²μ νμΈν μ μλ€.
μ κ·μμμλ λ¬Έμ ν΄λμ€ κΈ°λ₯μ μ§μνλ€.
const str = `
This_is my example string!!! Put Your Hands Up.
[1, 2, 3, 4, 5] is Array and Array is one of { Object }.
νκΈλ λλμ...?
`;
const wordRegExp = /\w+/g;
const numberRegExp = /\d+/g;
const spaceRegExp = /\s+/g;
console.log(str.match(wordRegExp));
console.log(str.match(numberRegExp));
console.log(str.match(spaceRegExp));
[
'This_is', 'my', 'example',
'string', 'Put', 'Your',
'Hands', 'Up', '1',
'2', '3', '4',
'5', 'is', 'Array',
'and', 'Array', 'is',
'one', 'of', 'Object'
]
[ '1', '2', '3', '4', '5' ]
[
'\n', ' ', ' ', ' ',
' ', ' ', ' ', ' ',
'\n', ' ', ' ', ' ',
' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ',
' ', ' ', ' ', '\n',
' ', '\n'
]
\wλ μΈλλ°(_)λ₯Ό ν¬ν¨νλ¬Έμ(0~9, a~z, A~Z)μ 맀μΉλλ λ¬Έμ ν΄λμ€μ΄λ€. μ«μμ μΈλλ°μ 맀μΉλλ€λ κ²μ μ£Όμν΄μΌνλ€.
\dμ \sλ κ²°κ³Όλ‘ λ³Ό μ μλ―μ΄ κ°κ° μ«μμ 곡백(μ€λ°κΏ ν¬ν¨)κ³Ό 맀μΉλλ€.
λ¬Έμ ν΄λμ€λ λ°λ ν΄λμ€λ‘λ μ¬μ© κ°λ₯νλ° μ¬μ©λ²μ λ€μκ³Ό κ°λ€.
const str = `
This_is my example string!!! Put Your Hands Up.
[1, 2, 3, 4, 5] is Array and Array is one of { Object }.
νκΈλ λλμ...?
`;
const notWordRegExp = /\W+/g;
const notNumberRegExp = /\D+/g;
const notSpaceRegExp = /\S+/g;
console.log(str.match(notWordRegExp));
console.log(str.match(notNumberRegExp));
console.log(str.match(notSpaceRegExp));
[
'\n',
' ',
' ',
' ',
'!!! ',
' ',
' ',
' ',
'.\n[',
', ',
', ',
', ',
', ',
'] ',
' ',
' ',
' ',
' ',
' ',
' ',
' { ',
' }.\nνκΈλ λλμ...?\n'
]
[
'\nThis_is my example string!!! Put Your Hands Up.\n[',
', ',
', ',
', ',
', ',
'] is Array and Array is one of { Object }.\nνκΈλ λλμ...?\n'
]
[
'This_is', 'my', 'example',
'string!!!', 'Put', 'Your',
'Hands', 'Up.', '[1,',
'2,', '3,', '4,',
'5]', 'is', 'Array',
'and', 'Array', 'is',
'one', 'of', '{',
'Object', '}.', 'νκΈλ',
'λλμ...?'
]
κ²°κ³Όμμ λ³Ό μ μλ―μ΄ κ°κ°μ λλ¬Έμλ‘ μ μ©νλ©΄ λ°λ κ°μ΄ 맀μΉλλ€.
λ¬Έμ ν΄λμ€μλ .λ μλλ°, .μ μ€λ°κΏμ μ μΈν λͺ¨λ κ°λ€κ³Ό 맀μΉλλ€.
const str = `
This_is my example string!!!
byebyebye
`;
const regExp0 = new RegExp('bye', 'g');
const regExp1 = /\bbye/g;
const regExp2 = /bye\b/g;
const regExp3 = /bye\B/g;
console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
console.log(str.match(regExp3));
[ 'bye', 'bye', 'bye' ]
[ 'bye' ]
[ 'bye' ]
[ 'bye', 'bye' ]
word boundaryλ λ¬Έμμ κ²½κ³μ 맀μΉλλ€. μ½κ² μκ°νλ©΄ \wκ° μλ λ μμ΄λΌκ³ μκ°νλ©΄ λλ€.
μΆμ²: https://ko.javascript.info/regexp-boundary
\bλ λ°λ ν΄λμ€μ²λΌ λλ¬Έμλ₯Ό μ΄μ©νλ©΄ word boundaryκ° μλ κ²κ³Ό 맀μΉλλλ° regExp3μ λ§€μΉ κ²°κ³Όλ₯Ό ν΅ν΄ νμΈν μ μλ€.
λμ΄ word boundaryκ° μλ 첫 byeμ λ λ²μ§Έ byeκ° λ§€μΉλλ€.
κ·Έλ°λ° μ \bλ₯Ό μ¨μΌν κΉ? \Wλ₯Ό μ¨λ λ ν λ° λ§μ΄λ€.
const str = `
This_is my example string!!!
byebyebye
`;
const regExp0 = new RegExp('bye', 'g');
const regExp1 = /\Wbye/g;
const regExp2 = /bye\W/g;
const regExp3 = /bye\W/g;
console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
console.log(str.match(regExp3));
[ 'bye', 'bye', 'bye' ]
[ '\nbye' ]
[ 'bye\n' ]
[ 'bye\n' ]
κ·Έ μ΄μ λ λ€μμ κ²°κ³Όμμ μ§μ νμΈν μ μλ€. π
const str = `
12345678
This_is my_example_
`;
const regExp0 = /[0-9]+/g;
const regExp1 = /[a-zA-Z]+/g;
const regExp2 = /[Tiem]+/g;
console.log(str.match(regExp0));
console.log(str.match(regExp1));
console.log(str.match(regExp2));
[ '12345678' ]
[ 'This', 'is', 'my', 'example' ]
[
'T', 'i', 'i',
'm', 'e', 'm',
'e'
]
λκ΄νΈλ₯Ό ν΅ν΄ Setκ³Ό range κΈ°λ₯μ νμ©ν μ μλ€. 0-9λ μ«μ 0 λΆν° 9, a-zλ a λΆν° z λ± λ€μνκ² νμ©ν μ μλ€.
λ€λ§ μμ€ν€ μ½λ κ°μ μ°Έμ‘°νλ μ¬μ©μ μ μν΄μΌνλ€.
regExp2 κ°μ κ²½μ°λ μνλ²³ T, i, e, m μ€ νλμ 맀μΉλλ€λ λ»μ΄λ€.
const str = `
010-898-0893
02-405-3412
010 405 3412
`;
const regExp = /\d{2,3}[- ]?\d{3}[- ]?\d{4}/g;
console.log(str.match(regExp));
[ '010-898-0893', '02-405-3412', '010 405 3412' ]
μλμλ 맀μΉλλ ν¨ν΄μ μΌμ κ°μλ₯Ό ννν λ μ¬μ©νλ€. μ€κ΄νΈ μμ μ«μλ₯Ό λ£μ΄μ μ¬μ©νλλ° μλ―Έλ λ€μκ³Ό κ°λ€.
{0, } 0 μ΄μ
{1, 3} 1 μ΄μ 3 μ΄ν
μλμλ κ°νΈνκ² νννλ shorthand λν μ 곡νλ€.
μλμ | μλ―Έ | νν |
---|---|---|
* | 0κ° μ΄μ | {0,} |
? | 0 λλ 1 | {0,1} |
+ | 1κ° μ΄μ | {1,} |