โถ ๋ฌธ์์ด์ ๊ฒ์ํ๊ณ ๋์ฒดํ๋ ๋ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ์ผ์ข ์ ํ์ ์ธ์ด(ํจํด)
โถ ์ ๊ท์, Regular Expresstion
์ญํ
- ๋ฌธ์ ๊ฒ์ (search)
- ๋ฌธ์ ๋์ฒด (replace)
- ๋ฌธ์ ์ถ์ถ (extract)
ํ ์คํธ ์ฌ์ดํธ
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๋ฅผ ์ฐพ๋๋ค.
'Front-End > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ฐ์ฐ์ (0) | 2022.12.30 |
---|---|
.ENV ์ค์ ํ๊ธฐ (feat. Vercel) (0) | 2022.12.22 |
๋ฐ์ดํฐ ํ์ - ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด (2) | 2022.12.15 |
๋ณ์ (Variable) (0) | 2022.12.09 |
์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ๋ฐ ํ๊ฒฝ๊ณผ ์คํ๋ฐฉ๋ฒ (0) | 2022.12.09 |