[JS] ์ •๊ทœํ‘œํ˜„์‹

Ch.31 ์ •๊ทœํ‘œํ˜„์‹

 

๐Ÿ”Ž ์ •๊ทœ ํ‘œํ˜„์‹์ด๋ž€?

๋ฌธ์ž์—ด์„ ๋Œ€์ƒ์œผ๋กœ ํŒจํ„ด ๋งค์นญ ๊ธฐ๋Šฅ์„ ์ œ๊ณต = ํŠน์ • ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ or ์ถ”์ถœ or ์น˜ํ™˜ํ•˜๋Š” ๊ธฐ๋Šฅ

const tel = 010-1234-5678;
const regExp = /^\d{3}-\d{4}-\d{4}/;

// tel์ด ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ํ…Œ์ŠคํŠธ
regExp.test(tel);  // true

 

 

๐Ÿ”จ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

1. ์ •๊ทœํ‘œํ˜„์‹ ๋ฆฌํ„ฐ๋Ÿด

→ ํŒจํ„ด๊ณผ ํ”Œ๋ž˜๊ทธ๋กœ ๊ตฌ์„ฑ

์ •๊ทœํ‘œํ˜„์‹ ๋ฆฌํ„ฐ๋Ÿด

const target1 = 'Is this all there is?';
const target2 = 'Is this all?';

// ํŒจํ„ด: is
// ํ”Œ๋ž˜๊ทธ: i => ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๊ณ  ๊ฒ€์ƒ‰
const regexp = /is/i;

regexp.test(target1); // true
regexp.test(target2); // true


2. RegExp ์ƒ์„ฑ์ž ํ•จ์ˆ˜

new RegExp(pattern[, flags])
// RegExp ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์‚ฌ์šฉ ์˜ˆ์‹œ
const target = 'Is this all there is?';
const regexp = new RegExp(/is/i);
regexp.test(target); // true

// ๋™์ ์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
const count = (str, char) => (str.match(new RegExp(char, 'gi')) ?? []).length;  // gi๋Š” ๋ฌธ์ž์—ด ์ „์ฒด์—์„œ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์•ˆํ•˜๊ณ 
count('Is this all there is?', 'is') // 3 -> is๊ฐ€ Is์— ํ•˜๋‚˜, this์— ํ•˜๋‚˜, ๋งˆ์ง€๋ง‰์— ํ•˜๋‚˜
count('Is this all there is?', 'xx') // 0 -> xx๋Š” ์—†์Œ

 

๐ŸŽฎRegExp์˜ ๋ฉ”์„œ๋“œ๋“ค

1. exec ๋ฉ”์„œ๋“œ

์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•ด์„œ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜, ์—†์œผ๋ฉด null ๋ฐ˜ํ™˜

๋ชจ๋“  ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•˜๋Š” g ํ”Œ๋ž˜๊ทธ๋กœ ํ•ด๋„ ๋ฌด์กฐ๊ฑด ์ฒซ๋ฒˆ์งธ ๋งค์นญ ๊ฒฐ๊ณผ๋งŒ์„ ๋ฐ˜ํ™˜ํ•จ!!!

const target = 'Is this all ther is?'
const regExp = /is/i
console.log(regExp.exec(target))

// ๊ฒฐ๊ณผ๊ฐ’
[
  0: "Is",               // ๋งค์น˜๋œ ๋ฌธ์ž์—ด ์ „์ฒด
  index: 0,              // ๋งค์น˜๊ฐ€ ๋ฐœ๊ฒฌ๋œ ์‹œ์ž‘ ์ธ๋ฑ์Šค ์œ„์น˜
  input: "Is this all ther is?",  // ๊ฒ€์ƒ‰์ด ์ˆ˜ํ–‰๋œ ์›๋ณธ ๋ฌธ์ž์—ด
  groups: undefined      // ์ •๊ทœ์‹์—์„œ ์บก์ฒ˜ ๊ทธ๋ฃน์ด ์žˆ์„ ๊ฒฝ์šฐ ์—ฌ๊ธฐ์— ์ €์žฅ๋จ
]

 

2. test ๋ฉ”์„œ๋“œ

์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•ด์„œ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ boolean์œผ๋กœ ๋ฐ˜ํ™˜

const target = 'Is this all ther is?'
const regExp = /is/i
console.log(regExp.test(target))  // true

 

3. match ๋ฉ”์„œ๋“œ(String์˜ ๋นŒ๋“œ์ธ ๊ฐ์ฒด → ๋ฌธ์ž์—ด์— ๋ถ™์—ฌ์„œ ์จ์•ผํ•จ)

๋Œ€์ƒ ๋ฌธ์ž์—ด๊ณผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ์˜ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

const target = 'Is this all there is?'
const regexp = /is/
console.log(target.match(regexp))

// ๊ฒฐ๊ณผ๊ฐ’
[
  0: "is",              // ๋งค์น˜๋œ ๋ฌธ์ž์—ด
  index: 5,             // ๋งค์น˜๊ฐ€ ๋ฐœ๊ฒฌ๋œ ์œ„์น˜ (this์˜ 'is')
  input: "Is this all there is?",    // ์›๋ณธ ๋ฌธ์ž์—ด
  groups: undefined     // ์บก์ฒ˜ ๊ทธ๋ฃน ๊ฒฐ๊ณผ (์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ)
]

exec ๋ฉ”์„œ๋“œ์™€ ๋น„์Šทํ•˜๊ฒŒ ๋ฐ˜ํ™˜๋˜์ง€๋งŒ, ๋‹ค๋ฅธ์ ์€ g ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ชจ๋“  ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•จ

const regexp = /is/g;  // g ํ”Œ๋ž˜๊ทธ ์ถ”๊ฐ€
console.log(target.match(regexp));
// ๊ฒฐ๊ณผ: ["is", "is"]  - 'this'์™€ ๋งˆ์ง€๋ง‰ 'is'์˜ ๋‘ ๊ฐœ๊ฐ€ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

 

 

๐Ÿšฉํ”Œ๋ž˜๊ทธ

์ •๊ทœ ํ‘œํ˜„์‹์—์„œ ๊ฒ€์ƒ‰ ๋ฐฉ์‹์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ

  • ํ•„์ˆ˜๊ฐ€ ์•„๋‹˜
  • ์ˆœ์„œ ์ƒ๊ด€ ์—†์ด ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ํ”Œ๋ž˜๊ทธ๊ฐ€ ์—†๋‹ค๋ฉด ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„๋จ + ์ฒซ๋ฒˆ์งธ ๋งค์นญ ๋Œ€์ƒ์ด ๊ฒ€์ƒ‰๋˜๋ฉด ์ข…๋ฃŒ

 

๐ŸงฉํŒจํ„ด

์ผ์ •ํ•œ ๊ทœ์น™์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋จ

  • /๋กœ ์—ด๊ณ  ๋‹ซ์œผ๋ฉฐ ๋”ฐ์˜ดํ‘œ๋Š” ์ƒ๋žต
  • ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ๋ฉ”ํƒ€๋ฌธ์ž or ๊ธฐํ˜ธ๋กœ ํ‘œํ˜„ ๊ฐ€๋Šฅ

์ž„์˜์˜ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰

์  ํ•˜๋‚˜(.) = ์ž„์˜์˜ ๋ฌธ์ž ํ•œ๊ฐœ

const target = 'Is this all there is?'
const regexp = /.../g
// ์ž„์˜์˜ 3๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„ํ•ด์„œ ์ „์—ญ ๊ฒ€์ƒ‰
target.match(regexp) // ['Is ', 'thi', 's a', 'll ', 'the', 're ', 'is?']

 

 

๋ฐ˜๋ณต ๊ฒ€์ƒ‰

(1) {m,n}์€ ์•ž์„  ํŒจํ„ด์ด ์ตœ์†Œ m๋ฒˆ, ์ตœ๋Œ€ n๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด

  • ์—ฌ๊ธฐ์„œ {m, n}์ด๋Ÿฐ์‹์œผ๋กœ ์ค‘๊ฐ„์— ๋„์–ด์“ฐ๊ธฐํ•˜๋ฉด ๋™์ž‘ ์•ˆํ•จ
  • {n} ์ด๋ ‡๊ฒŒ ํ•˜๋‚˜๋งŒ ์žˆ์œผ๋ฉด {n,n}๊ณผ ๋™์ผ
  • {n,} ์ด๋ ‡๊ฒŒ ๋’ค์—๊ฐ€ ์ƒ๋žต๋˜์–ด ์žˆ์œผ๋ฉด {1,∞}์™€ ๋™์ผ = ์ตœ์†Œ n๋ฒˆ
const target = 'A AA B BB Aa Bb AAA'
const regExp = /A{1,2}/g
// A๊ฐ€ ์ตœ์†Œ 1๋ฒˆ, ์ตœ๋Œ€ 2๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด ์ „์—ญ ๊ฒ€์ƒ‰
target.match(regExp)  // ['A', 'AA', 'A', 'AA', 'A']

 

(2) +๋Š” ์•ž์„  ํŒจํ„ด์ด ์ตœ์†Œ ํ•œ๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด

์ฆ‰, +๋Š” {1,}๊ณผ ๊ฐ™๋‹ค.

const target = 'A AA B BB Aa Bb AAA'
const regExp = /A+/g
// A๊ฐ€ ์ตœ์†Œ 1๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด ์ „์—ญ ๊ฒ€์ƒ‰
target.match(regExp)   // ['A', 'AA', 'A', 'AAA']

 

(3) ?๋Š” ์ตœ๋Œ€ ํ•œ๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด

์ฆ‰, ?๋Š” {0,1}๊ณผ ๋™์ผํ•จ

const target = 'color colour colouur'
// colo๊นŒ์ง€๋Š” ์žˆ์–ด์•ผํ•˜๊ณ , u๋Š” ์ตœ๋Œ€ ํ•œ๋ฒˆ(์—†์–ด๋„ ๋จ), r์ด ์žˆ์–ด์•ผํ•จ
const regExp = /colou?r/g
target.match(regExp)  // ['color', 'colour']

 

 

OR ๊ฒ€์ƒ‰

  • | ์‚ฌ์šฉ
  • [ ] ์‚ฌ์šฉ
const target = 'A AA B BB Aa Bb'

// ์ „์—ญ์—์„œ A ๋˜๋Š” B ๊ฒ€์ƒ‰
const regExp1 = /A|B/g
target.match(regExp1) // ['A', 'A', 'A', 'B', 'B', 'B', 'A', 'B']

// ๋ถ„ํ•ด ์•ˆํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋ ค๋ฉด +๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉ
const regExp2 = /A+|B+/g
target.match(regExp2) // ['A', 'AA', 'B', 'BB', 'A', 'B']

// ๊ทผ๋ฐ 2๋ฒˆ์„ []๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋ ‡๊ฒŒ๋„ ์“ธ ์ˆ˜ ์žˆ์Œ
const regExp3 = /[AB]+/g
target.match(regExp3) // ['A', 'AA', 'B', 'BB', 'A', 'B']

// ๋งŒ์•ฝ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•˜๊ณ  ์‹ถ์œผ๋ฉด []์•ˆ์— -๋ฅผ ์‚ฌ์šฉ
const regExp4 = /[A-Z]+/g
// ์ด ๊ฒฝ์šฐ์—” ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ ๊ฒ€์ƒ‰
target.match(regExp4)  // ['A', 'AA', 'B', 'BB', 'A', 'B']

// ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„ ์•ˆํ• ๋ ค๋ฉด ์ด๋ ‡๊ฒŒ
const regExp5 = /[A-Za-z]+/g
target.match(regExp5)  // ['A', 'AA', 'B', 'BB', 'Aa', 'Bb']

// ์ด๊ฑธ ํ™œ์šฉํ•ด์„œ ์ˆซ์ž๋„ ๊ฐ€๋Šฅ
const newTarget = 'AA BB 12,345'
const regExp6 = /[0-9]+/g
newTarget.match(regExp6) // ['12', '345']

 

[A-Z], [0-9]์ด๋ ‡๊ฒŒ ์ผ์ผํžˆ ์จ์ฃผ๋Š” ๋Œ€์‹ , \d, \w๋“ฑ์„ ์ด์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์“ธ ์ˆ˜๋„ ์žˆ์Œ

const target = 'AA BB 12,345'

// \d๋Š” ์ˆซ์ž
const regExp1 = /[\d,]+/g    // ์ˆซ์ž ๋˜๋Š” ','๊ฐ€ ํ•œ๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด
target.match(regExp1)  // ['12,345']

// \D๋Š” ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž
const regExp2 = /[\D,]+/g
target.match(regExp2)  // ['AA BB ', ',']

// \w๋Š” ์•ŒํŒŒ๋ฒณ, ์ˆซ์ž, ์–ธ๋”์Šค์ฝ”์–ด [A-Za-z0-9_]์™€ ๊ฐ™์Œ
const regExp3 = /[\w,]+/g
target.match(regExp3)  // ['AA', 'BB', '12,345']

// \W๋Š” \w์˜ ๋ฐ˜๋Œ€
const regExp4 = /[\W,]+/g
target.match(regExp4)  // [' ', ' ', ',']

 

 

NOT๊ฒ€์ƒ‰

[ ] ์•ˆ์—์„œ์˜ ^๋Š” not์„ ์˜๋ฏธํ•จ(ex. [^0-9]๋Š” ์ˆซ์ž๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์ž)

const target = 'AA BB 12 Aa Bb'
// ์ˆซ์ž ์ œ์™ธ ๊ฒ€์ƒ‰
const regExp = /[^0-9]+/g
target.match(regExp)  // ['AA BB ', ' Aa Bb']

 

 

์‹œ์ž‘ ์œ„์น˜๋กœ ๊ฒ€์ƒ‰

[ ] ๋ฐ–์—์„œ์˜ ^๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘

const target = 'https://memezz.tistory.com'
const regExp = /^https/g
regExp.test(target)  // true

 

 

๋งˆ์ง€๋ง‰ ์œ„์น˜๋กœ ๊ฒ€์ƒ‰

$๋Š” ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰์„ ์˜๋ฏธ

const target = 'https://memezz.tistory.com'
const regExp = /com$/g
regExp.test(target)  // true