Plant seed
article thumbnail

โ–ถ ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๋Œ€์ฒดํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ผ์ข…์˜ ํ˜•์‹ ์–ธ์–ด(ํŒจํ„ด)

โ–ถ ์ •๊ทœ์‹, Regular Expresstion

 

 

์—ญํ• 

  • ๋ฌธ์ž ๊ฒ€์ƒ‰ (search)
  • ๋ฌธ์ž ๋Œ€์ฒด (replace)
  • ๋ฌธ์ž ์ถ”์ถœ (extract)

 

 

ํ…Œ์ŠคํŠธ ์‚ฌ์ดํŠธ

https://regexr.com/

 

RegExr: Learn, Build, & Test RegEx

RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

regexr.com

 

 

์ •๊ทœ์‹ ์ƒ์„ฑ

// ์ƒ์„ฑ์ž
new RegExp('ํ‘œํ˜„', '์˜ต์…˜')
new RegExp('[a-z]', 'gi')

//๋ฆฌํ„ฐ๋Ÿด
/ํ‘œํ˜„/์˜ต์…˜
/[a-z]/gi

 

ex) ์ƒ์„ฑ์ž ๋ฐฉ์‹

const srt = `
    010-1234-5678
    thesecon@gmail.com
    https://www.omdbapi.com/?apikey=7035c60c&s=frozen
    The quick brown fox jumps over the lazy dog.
    abbcccdddd
`

const regexp = new RegExp('the', 'g') // ๋ชจ๋“  'the'๋ฅผ ์ฐพ๋Š”๋‹ค.
console.log(str.match(regexp)) // (2) ["the", "the"]

const regexp = new RegExp('the', 'gi') // ๋Œ€์†Œ๋ฌธ์ž ๊ฐ€๋ฆฌ์ง€ ์•Š๊ณ  ๋ชจ๋“  'the'๋ฅผ ์ฐพ๋Š”๋‹ค.
console.log(str.match(regexp)) // (3) ["the", "The", "the"]

 

ex) ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹

const srt = `
    010-1234-5678
    thesecon@gmail.com
    https://www.omdbapi.com/?apikey=7035c60c&s=frozen
    The quick brown fox jumps over the lazy dog.
    abbcccdddd
`

const regexp = /the/g // ๋ชจ๋“  'the'๋ฅผ ์ฐพ๋Š”๋‹ค.
console.log(str.match(regexp)) // (2) ["the", "the"]

const regexp = /the/gi // ๋Œ€์†Œ๋ฌธ์ž ๊ฐ€๋ฆฌ์ง€ ์•Š๊ณ  ๋ชจ๋“  'the'๋ฅผ ์ฐพ๋Š”๋‹ค.
console.log(str.match(regexp)) // (3) ["the", "The", "the"]

 

 

๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ ๋ฌธ๋ฒ• ์„ค๋ช…
test ์ •๊ทœ์‹.test(๋ฌธ์ž์—ด) ์ผ์น˜ ์—ฌ๋ถ€(Boolean) ๋ฐ˜ํ™˜
match ๋ฌธ์ž์—ด.match(์ •๊ทœ์‹) ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์˜ ๋ฐฐ์—ด(Array) ๋ฐ˜ํ™˜
replace ๋ฌธ์ž์—ด.replace(์ •๊ทœ์‹, ๋Œ€์ฒด๋ฌธ์ž) ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๋Œ€์ฒด
const srt = `
    010-1234-5678
    thesecon@gmail.com
    https://www.omdbapi.com/?apikey=7035c60c&s=frozen
    The quick brown fox jumps over the lazy dog.
    abbcccdddd
`

const regexp = /fox/gi
console.log(regexp.test(str)) // true

const regexp = /HEROPY/gi
console.log(regexp.test(str)) // false

const regexp = /fox/gi
console.log(str.replace(regexp, 'AAA')) // ์œ„์˜ ๋ฌธ์žฅ ์ค‘ 'fox' ๋‹จ์–ด๋ฅผ 'AAA'๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

const regexp = /fox/gi
console.log(str.replace(regexp, 'AAA')) // ์œ„์˜ ๋ฌธ์žฅ ์ค‘ 'fox' ๋‹จ์–ด๋ฅผ 'AAA'๋กœ ๋Œ€์ฒดํ•œ๋‹ค.

str = str.replace(regexp, 'AAA') // str์„ ์ƒ์ˆ˜์ธ const๋ฅผ let์œผ๋กœ ๋ณ€๊ฒฝ ํ›„ ํ• ๋‹นํ•˜๋ฉด ์›๋ณธ์— ์žฌํ• ๋‹น ํ•  ์ˆ˜ ์žˆ๋‹ค.
console.log(str)

 

 

ํ”Œ๋ž˜๊ทธ (์˜ต์…˜)

ํ”Œ๋ž˜๊ทธ ์„ค๋ช…
g ๋ชจ๋“  ๋ฌธ์ž ์ผ์น˜ (global)
i ์˜์–ด ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ ์•Š๊ณ  ์ผ์น˜ (ignore case)
m ์—ฌ๋Ÿฌ ์ค„ ์ผ์น˜ (multi line)
const srt = `
    010-1234-5678
    thesecon@gmail.com
    https://www.omdbapi.com/?apikey=7035c60c&s=frozen
    The quick brown fox jumps over the lazy dog.
    abbcccdddd
`

// ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž(Escape Character)๋ž€ \(๋ฐฑ์Šฌ๋ž˜์‹œ)๊ธฐํ˜ธ๋ฅผ ํ†ตํ•ด ๋ณธ๋ž˜์˜ ๊ธฐ๋Šฅ์—์„œ ๋ฒ—์–ด๋‚˜ ์ƒํƒœ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๋ฌธ์ž๋ฅผ ๋งํ•œ๋‹ค.
console.log(str.match(/\.$/gim)) // .(๋งˆ์นจํ‘œ)๋ฅผ ์ฐพ๋Š”๋‹ค.

 

 

ํŒจํ„ด (ํ‘œํ˜„)

ํŒจํ„ด ์„ค๋ช…
^ab ์ค„(Line) ์‹œ์ž‘์— ์žˆ๋Š” ab์™€ ์ผ์น˜
ab$ ์ค„(Line) ๋์— ์žˆ๋Š” ab์™€ ์ผ์น˜
. ์ž„์ด์˜ ํ•œ ๋ฌธ์ž์™€ ์ผ์น˜
a|b a ๋˜๋Š” b์™€ ์ผ์น˜
ab? b๊ฐ€ ์—†๊ฑฐ๋‚˜ b์™€ ์ผ
{3} 3๊ฐœ ์—ฐ์† ์ผ์น˜
{3, } 3๊ฐœ ์ด์ƒ ์—ฐ์† ์ผ์น˜
{3, 5} 3๊ฐœ ์ด์ƒ 5๊ฐœ ์ดํ•˜(3~5๊ฐœ) ์—ฐ์† ์ผ์น˜
[abc] a ๋˜๋Š” b ๋˜๋Š” c
[a-z] a๋ถ€ํ„ฐ z์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(์˜์–ด ์†Œ๋ฌธ์ž)
[A-Z] A๋ถ€ํ„ฐ Z์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(์˜์–ด ๋Œ€๋ฌธ์ž)
[0-9] 0๋ถ€ํ„ฐ 9์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(์ˆซ์ž)
[๊ฐ€-ํžฃ] ๊ฐ€๋ถ€ํ„ฐ ํžฃ ์‚ฌ์ด์˜ ๋ฌธ์ž ๊ตฌ๊ฐ„์— ์ผ์น˜(ํ•œ๊ธ€)
\w 63๊ฐœ ๋ฌธ์ž(Word, ๋Œ€์†Œ์˜๋ฌธ 52๊ฐœ + ์ˆซ์ž10๊ฐœ + _)์— ์ผ์น˜
\b 63๊ฐœ ๋ฌธ์ž์— ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๋ฌธ์ž ๊ฒฝ๊ณ„(Boundary)
\d ์ˆซ์ž(Digit)์— ์ผ์น˜
\s ๊ณต๋ฐฑ(Space, Tab ๋“ฑ)์— ์ผ์น˜
(?=) ์•ž์ชฝ ์ผ์น˜(Lookahead)
(?<=) ๋’ค์ชฝ ์ผ์น˜(Lookbehind)
const srt = `
    010-1234-5678
    thesecon@gmail.com
    https://www.omdbapi.com/?apikey=7035c60c&s=frozen
    The quick brown fox jumps over the lazy dog.
    abbcccdddd
`

console.log(str.match(/d$/gm)) // ๋๋ถ€๋ถ„ d๋ฅผ ์ฐพ๋Š”๋‹ค.

console.log(str.match(/^t/gim)) // ์‹œ์ž‘๋ถ€๋ถ„ t,T๋ฅผ ์ฐพ๋Š”๋‹ค.