π₯ [JavaScript] μ κ·ννμμ λν΄ μμ보μ
κ°μ
μ¬νκ» μ κ·ννμμ validation
μ μ μ©ν λ λ°μ μ¨λ³΄μ§ μμλ€.
νμ§λ§ μμ¦ javascript
λ‘ μ½λ©ν
μ€νΈλ₯Ό μ€λΉνλ€ λ³΄λ©΄
μ κ·ννμμ μ¬μ©νλ μΌμ΄ μ¦μ κ² κ°μ μ λλ‘ μμλ³΄λ € νλ€.
μ κ·ννμμ΄λ?
μ κ·ννμ (RegExp, Regular Expression)
λ¬Έμλ₯Ό κ²μνκ±°λ λ체, μΆμΆ ν λ μ¬μ©νλ μΌμ’ μ ν¨ν΄
μ½κ² λ§ν΄ λ¬Έμμ΄μ μ½κ² μ κ·Όν μ μλ ν¨ν΄μ΄λΌκ³ 보면 λλ€.
μ¬μ© λ°©λ²
μ κ·ννμμ ν¬κ² λ κ°μ§ λ°©λ²μ΄ μλλ° μμ±μ λ°©μ
κ³Ό 리ν°λ΄ λ°©μ
μ΄ μλ€.
1
2
3
4
5
6
7
8
9
// 1. μμ±μ λ°©μ
new RegExp("νν", "μ΅μ
");
new RegExp("[a-z]", "gi");
// 2. 리ν°λ΄ λ°©μ
/νν/μ΅μ
/[a-z]/gi
μ¬κΈ°μ λ¨Όμ μμ±μ λ°©μ
μΌλ‘ μμ보μ
μμ±μ λ°©μ
1
2
3
4
5
6
7
8
9
10
11
const str = `
010-1234-5678
thesecond@gmail.com
hello world!
https://ezurno.github.io/
The quick brown fox jumps over the lazy dog.
`;
const reg = new RegExp("the", "");
// λ³κ²½μ΄ λ λΆλΆ
console.log(str.match(reg));
μ΄λ¬ν ν¨μκ° μ‘΄μ¬νλ€κ³ μΉμ
ν΄λΉ ν¨μλ₯Ό λμμν€λ©΄ μ½μμμλ
1
["the", index: 15, input: ~~~~] //[`μ°Ύμ κ°`, `κ°μ μμΉ`, `μ
λ ₯ν κ°`]
μ΄λ κ² λ‘κ·Έκ° μ°νλμ€λλ° κ°κ° μ°Ύμ κ°
, κ°μ μμΉ
, μ
λ ₯ν κ°
μΌλ‘
λ°°μ΄ λ°μ΄ν°λ‘ λνλλ€.
κ·ΈλΌ μ΄λ²μ μ΅μ
λ°μ΄ν°μ g
λ₯Ό μΆκ° ν΄λ³΄μ
1
2
3
4
5
6
const reg = new RegExp("the", "g");
console.log(str.match(reg));
// ["the", "the"]
// λ§€μΉ λ λ°μ΄ν°λ€μ λ°°μ΄λ‘ λ°ν
// λ¨ λμλ¬Έμ ꡬλ³μ νλ―λ‘ The λ 맀μΉλμ§ μμ
κ·ΈλΌ μ΄λ²μ λμλ¬Έμ ꡬλΆμ νμ§ μμλ €λ©΄ μ΄λ»κ² ν΄μΌν κΉ?
κ°λ¨νκ² μ΅μ
λ°μ΄ν°μ i
λ₯Ό μΆκ°νλ©΄ λλ€.
1
2
3
4
5
const reg = new RegExp("the", "gi");
console.log(str.match(reg));
// ["the", "The", "the"]
// λ§€μΉ λ λ°μ΄ν°λ€μ λ°°μ΄λ‘ λ°ν
리ν°λ΄ λ°©μ
μ΄λ¬ν μ κ·ννμμ μμ±ν λ, 리ν°λ΄ λ°©μ
μ μ¬μ©νλ©΄ λ κ°λ¨νκ²
μ½λλ₯Ό μμ±ν μ μλ€.
1
2
3
4
5
// 1) const reg = new RegExp("the", "g");
const reg = /the/g;
// 2) const reg = new RegExp("the", "gi");
const reg = /the/gi;
κ·ΈλΌ μ΄λ²μλ μ κ·ννμμμ μ£Όλ‘ μ¬μ©νλ ν¨μλ₯Ό μμ보μ
μ κ·ννμ ν¨μ
μμ£Ό μ¬μ©νλ ν¨μλ ν¬κ² 3κ°μ§κ° μλ€.
test | match | replace |
---|---|---|
μΌμΉμ¬λΆ λ°ν | μΌμΉνλ λ¬Έμ λ°°μ΄ λ°ν | μΌμΉνλ λ¬Έμλ₯Ό λ체 |
μ κ·μ | λ¬Έμμ΄ | λ¬Έμμ΄ |
test
λ μ κ·μμ μ¬μ©νκ³
match
, replace
λ λ¬Έμμ΄μ μ¬μ©νλ ν¨μλ€.
1
2
3
4
5
6
7
8
9
10
11
12
const str = `
010-1234-5678
thesecond@gmail.com
hello world!
https://ezurno.github.io/
The quick brown fox jumps over the lazy dog.
`;
const reg = /fox/gi;
console.log(reg.test(str)); // false
console.log(str.match(reg)); // ["fox"]
console.log(str.replace(reg, "cat")); // fox λ₯Ό cat μΌλ‘ κ΅μ²΄
νλκ·Έ
μ κ·ννμμ μ¬μ©ν λ μ΅μ λ°μ΄ν°μ μΆκ°νλ κ°
μ΄λ¬ν νλκ·Έμλ 3κ°μ§κ° μλ€.
g | i | m |
---|---|---|
λͺ¨λ λ¬Έμ μΌμΉ | μμ΄ λμλ¬Έμλ₯Ό ꡬλΆνμ§ μκ³ μΌμΉ | μ¬λ¬μ€ μΌμΉ, κ°κ° μ€μ μμκ³Ό λμΌλ‘ μΈμ |
νλκ·Έλ₯Ό μ¬μ©νκΈ° μ μ λ¨Όμ escape-character
μ λν΄ μμλ³΄κ³ λ€μ΄κ°λ €νλ€.
escape-character
μ κ·μμ΄ κ°μ§κ³ μλ κΈ°λ³Έμ μΈ κΈ°λ₯μ νμΆν΄ λ¬Έμλ‘μμ κΈ°λ₯λ§μ μ¬μ©νλ λ¬Έμ
μμμΌλ‘λ \
, λμΌλ‘λ $
λ₯Ό μ¬μ©ν΄ λνλΈλ€.
1
2
3
// \$ λ₯Ό μ¬μ©
console.log(str.match(/\.$/gi));
// . μ μ°Ύλλ€λ μλ―Έ
1
2
3
4
5
6
7
8
9
10
11
12
13
const str = `
010-1234-5678
thesecond@gmail.com
hello world!
https://ezurno.github.io/
The quick brown fox jumps over the lazy dog.
`;
console.log(str.match(/the/)); // the μ μΈλ±μ€ λ° μ 보 λ°°μ΄
console.log(str.match(/the/g)); // the μΌμΉ
console.log(str.match(/the/gi)); // λμλ¬Έμλ₯Ό ꡬλΆνμ§ μκ³ the μ μΌμΉ
console.log(str.match(/\.$/gi)); // μ΄μ€μΌμ΄ν κ°μΌλ‘ . μ μ°Ύμ
console.log(str.match(/\.$/gim)); // μ€μ λλ μ . μ μ°Ύμ
ν¨ν΄
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
^ab // μ€ μμμ μλ ab μ μΌμΉ
ab$ // μ€ λμ μλ ab μ μΌμΉ
. // μμμ ν λ¬Έμμ μΌμΉ
a|b // a λλ b μ μΌμΉ
ab? // b κ° μκ±°λ b μ μΌμΉ
{3} // 3κ° μ°μ μΌμΉ
{3, } // 3κ° μ΄μ μ°μ μΌμΉ
{3, 5} // 3κ° μ΄μ 5κ° μ΄ν μΌμΉ
+ // 1ν μ΄μ μ°μ μΌμΉ {1, }
[ab] // a λλ b
[a-z] // a λΆν° z κ°μ λ¬Έμ ꡬκ°μ μΌμΉ
[A-Z] // A λΆν° Z κ°μ λ¬Έμ ꡬκ°μ μΌμΉ
[0-9] // 0 λΆν° 9 κΉμ§μ μ«μ ꡬκ°μ μΌμΉ
[κ°-ν£] // νκΈ
\w // 63 κ°μ λ¬Έμ(λμμλ¬Έ 52κ° + μ«μ 10κ° + _)μ μΌμΉ
\b // 64 κ°μ λ¬Έμμ μΌμΉνμ§ μλ λ¬Έμ κ²½κ³ (boundary)
\d // μ«μμ μΌμΉ (digit)
\s // 곡백 (space, tab) λ±μ μΌμΉ
(?:) // κ·Έλ£Ή μ§μ
(?=) // μμͺ½ μΌμΉ
(?<=) // λ·μͺ½ μΌμΉ
λ±μ΄ μλ€.
λ§μΉλ©°β¦
μ κ·ννμβ¦ μκ°λ³΄λ€ μ°κΈ°λ νΈν΄μ μ’μλλ°
νμμ λ³΄μ§ λͺ»νλ λ¬Έλ²λ μ²μ λ³΄κ² λμ΄ μκ°λ³΄λ€ λν μΌ νλ€λ κ²μ μκ² λμλ€.
μμΌλ‘ μ κ·ννμμ μ¬μ©ν΄μ λ¬Έμμ΄μ μ’ λ μ½κ²
κ΄λ¦¬ν μ μμ κ² κ°λ€. νΈνλ€