str1.match(/\b\w{1,}\b/g)
μ§γ γ λ¨ νκΈμλ μ΄ν΄ν μ μμλ μ κ·ννμ ..
ν¨μΊ μμ μ κ³΅ν΄ μ€ νμκ°μ κ°μλ§μΌλ‘ μ΄μ μ‘°κΈμ μ΄ν΄νκ³ μΈ μ μκ² λλ€..!!
/νν/μ΅μ
/abc/g // "abc"λΌλ λ¬Έμμ΄μ μ°Ύκ³ μν¨ g(global)μ΅μ
μ μ€μ νμ¬ κ²μ
new RegExp()
μμ±μ ν¨μλ₯Ό μ¬μ© //new RegExp('νν(ν¨ν΄)', 'μ΅μ
')
const regexp = new RegExp('[a-z]','gi')
/
λ‘ κ°μΈμ§ ν¨ν΄μ 리ν°λ΄λ‘ μ¬μ© // /νν/μ΅μ
const regexp = /[a-z]/gi
μ κ·μ.test(λ¬Έμμ΄)
const str = "The quick brown fox jumps over the lazy dog."
// μ 체 λ¬Έμμ΄ μ€ "the" λ₯Ό νμ
const regexp = /the/g
// strλ³μμ μ 체 λ¬Έμμ΄ μ€ "the"λ₯Ό νμ ν μΌμΉνλ λ¬Έμμ΄μ μ¬λΆλ₯Ό λ°ν
console.log(regexp.test(str)) //true
λ¬Έμμ΄.match(μ κ·μ)
const str = "The quick brown fox jumps over the lazy dog."
// μ 체 λ¬Έμμ΄ μ€ λμλ¬Έμμ κ΅¬λΆ μμ΄ "the" λ₯Ό νμ
const regexp = /the/gi
// strλ³μμ μ 체 λ¬Έμμ΄ μ€ "the"λ₯Ό νμ ν μΌμΉνλ λ¬Έμλ€μ λ°°μ΄ννλ‘ λ°ν
console.log(regexp.test(str)) // ['The', 'the']
λ¬Έμμ΄.replace(μ κ·μ, λ체문μ)
const str = "The quick brown fox jumps over the lazy dog."
// μ 체 λ¬Έμμ΄ μ€ λμλ¬Έμμ κ΅¬λΆ μμ΄ "the" λ₯Ό νμ
const regexp = /the/gi
// str λ³μμ μ 체 λ¬Έμμ΄ μ€ λμλ¬Έμμ κ΅¬λΆ μμ΄ "the" λ₯Ό νμ νμ¬ "TTHHEE"λ‘ μ¬λ°°μΉ(replace)
console.log(str.replace(regexp1,'TTHHEE')) //"TTHHEE quick brown fox jumps over TTHHEE lazy dog."
/νν/μ΅μ
μ μ κ·μ ν¨ν΄μμ λ€μ μ΅μ
μ리μ λ€μ΄κ°λ©° μ¬λ¬κ°μ§ μ΅μ
μ νκΊΌλ²μ λ£μ μ μμex)
/νν/
: μ무 μ΅μ (νλκ·Έ)λ λΆμ¬νμ§ μμ
/νν/g
: g μ΅μ (νλκ·Έ)λ§ λΆμ¬
/νν/gi
: g μ΅μ (νλκ·Έ)κ³Ό i μ΅μ λΆμ¬
/νν/gim
: g μ΅μ (νλκ·Έ)κ³Ό i μ΅μ κ³Ό m μ΅μ λΆμ¬
const str = "the quick brown fox jumps over the lazy dog."
// "o" λ₯Ό νμ ( μ무 νλκ·Έλ λΆμ¬νμ§ μμ )
const regexp = /o/
// strλ³μμμ "o"λ₯Ό νμ ν μΌμΉνλ λ¬Έμλ₯Ό λ°ν(match)
// g μ΅μ
μ λΆμ¬νμ§ μμ μ²μμΌλ‘ λ§λλ νλμ 'o'λ§ λ°ν
console.log(regexp.match(str)) // ['o']
// μ 체 λ¬Έμμ΄ μ€ "o" λ₯Ό νμ (g νλκ·Έ)
const globalRegexp = /o/g
// strλ³μμ μ 체 λ¬Έμμ΄ μ€ "o"λ₯Ό νμ ν μΌμΉνλ λ¬Έμλ€μ λ°°μ΄ννλ‘ λ°ν(match)
// g μ΅μ
μ λΆμ¬νμ¬ ν΄λΉ λ¬Έμμ΄μ λͺ¨λ "o" λ°ν
console.log(globalRegexp.match(str)) // ['o','o','o','o']
const str = "The quick brown fox jumps over the lazy dog."
// μ 체 λ¬Έμμ΄ μ€(g) "the" λ₯Ό νμ
const regexp = /the/g
console.log(regexp.match(str)) // ['the']
// μ 체 λ¬Έμμ΄ μ€(g) λμλ¬Έμλ₯Ό ꡬλΆνμ§ μκ³ (i) "the" λ₯Ό νμ
const ignoreRegexp = /the/gi
console.log(ignoreRegexp.match(str)) // ['The','the']
const str = `The quick brown fox jumps over the lazy dog
My faxed joke won a pager in the cable TV quiz show
Sphinx of black quartz, judge my vow`
// μ 체 λ¬Έμμ΄ μ€ "w"λ‘ λλλ λ¬Έμμ΄μ "w"λ₯Ό λ°ν
// mνλκ·Έλ₯Ό λΆμ¬νμ§ μμΌλ©΄ μ 체 λ¬Έμμ΄μ νλμ λ¬Έμμ΄λ‘ μΈμνμ¬ 'w'λ‘ λλλ λ¬Έμμ΄μ΄ νλλΌκ³ νλ¨
console.log(str.match(/w$/g)) //['w']
// μ 체 λ¬Έμμ΄ μ€ "w"λ‘ λλλ λ¬Έμμ΄μ "w"λ₯Ό λ°ν
// mνλκ·Έλ₯Ό λΆμ¬νμ¬ μ€λ¨μλ‘ λ¬Έμμ΄μ λλμ΄ μΈμ. 'w'λ‘ λλλ λ¬Έμμ΄μ λͺ¨λ μ°Ύμ
console.log(str.match(/w$/gm)) //['w', 'w']
λ€μν νΉμκΈ°νΈμ ν¨ν΄μ ν΅ν΄ λ¬Έμμ΄μ κ²μνλ λ°©μ (μΌμΉν¨ν΄)μ μ§μ ν μ μλ€
const str = `The quick brown fox jumps over the lazy dog
My faxed joke won a pager in the cable TV quiz show
Sphinx of black quartz, judge my vow`
// 'the'λ‘ μμνλ λ¬Έμμ΄ νμ
console.log(str.match(/^the/gim)) // ['The']
// ^ννμ μμ΄ κ²μ μ λͺ¨λ 'the' νμ
console.log(str.match(/the/gim)) // ['The','the','the']
const str = `The quick brown fox jumps over the lazy dog
My faxed joke won a pager in the cable TV quiz show
Sphinx of black quartz, judge my vow`
// 'the'λ‘ μμνλ λ¬Έμμ΄ νμ
console.log(str.match(/^the/gim)) // ["The"]
const str = "The quick brown fox jumps over the lazy dog."
// 'fox'λλ 'dog' λͺ¨λ νμ
console.log(str.match(/fox|dog/g)) //['fox', 'dog']
const str = `
https://fastcampus.app/
http://localhost:1234/
`
// 's'λ€μ ?ννμμ λΆμ¬ 's'κ° μκ±°λ μλ 'http'λ₯Ό νμ
console.log(str.match(/https?/g)) //['https', 'http']
const str = "The quick brown fox"
// . : λμ΄μ°κΈ°λ₯Ό ν¬ν¨ ν λͺ¨λ λ¬Έμμ΄ νμ
console.log(str.match(/./g))
// ['T', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', ' ', 'b', 'r', 'o', 'w', 'n', ' ', 'f', 'o', 'x']
//μλμ κ°μ΄ μ€κ°λ¬Έμλ₯Ό μ νν μ μ μμ λ μ¬μ© ν μ μμ
console.log(str.match(/b...n/g)) //['brown']
console.log(str.match(/f../g)) //['fox']
console.log(str.match(/.he/g)) //['The']
const str = "abc1212@naver.com"
// \w+ : 63κ° λ¬Έμμ μΌμΉνλ μμμ λ¬Έμκ° 1κ°μ΄μ μ°μλ¨.
console.log(str.match(/\w+/g)) //[abc1212, naver, com]
const str = 'xxxx'
// 'x'κ° 2λ² μ°μμΌλ‘ μΌμΉνλ λ¬Έμ νμ
console.log(str.match(/x{2}/g)) //['xx', 'xx']
const str = 'xxxx'
// 'x'κ° 2λ² μ΄μ μ°μμΌλ‘ μΌμΉνλ λ¬Έμ νμ
console.log(str.match(/x{2,}/g)) //['xxxx']
const str = 'xxxx'
// 'x'κ° 2λ²μ΄μ 3λ² μ΄ν μ°μμΌλ‘ μΌμΉνλ λ¬Έμ νμ
console.log(str.match(/x{2,3}/g)) //['xxx']
const str = 'abc1212@naver.com'
// μ΄λ©μΌ νμ μ κ·μ
// .+ : κΈΈμ΄,λ¬Έμ μκ΄μμ΄ λͺ¨λ λ¬Έμ μΌμΉ (μμ΄λ)
// @
// .+ : κΈΈμ΄,λ¬Έμ μκ΄μμ΄ λͺ¨λ λ¬Έμ μΌμΉ (μ΄λ©μΌμ£Όμκ°)
// \. : .
// .{2,} : 2μ리 μ΄μ μ°μλλ μμμ λ¬Έμ (com, net λ±)
console.log(str.match(/.+@.+\..{2,}/gm)) //abc1212@naver.com
const str = "The quick brown fox jumps over the lazy dog."
// [fox] : 'f' λλ 'o' λλ 'x' λͺ¨λ νμ
console.log(str.match(/[fox]/g)) //['o', 'f', 'o', 'x', 'o', 'o']
const str = 'http://localhost:1234/'
// a-z μ¬μ΄μ λͺ¨λ μνλ²³ νμ (μλ¬Έμ)
console.log(str.match(/[a-z]/g)) // ['h', 't', 't', 'p', 'l', 'o', 'c', 'a', 'l', 'h', 'o', 's', 't']
const str = 'Hello World!'
// A-Z μ¬μ΄μ λͺ¨λ μνλ²³ νμ (λλ¬Έμ)
console.log(str.match(/[A-Z]/g)) // ['H', 'W']
(\ : λ°±μ¬λμ κΈ°νΈμ λλ€)
const str = '010-1234-5678'
//1κΈμ μ΄μ μ°μλλ({1, }) 0-9μ¬μ΄μ μ«μ
console.log(str.match(/[0-9]{1,}/g)) //['010', '1234', '5678']
console.log(str.match(/\d{1,}/g)) //['010', '1234', '5678']
const str = 'λν΄λ¬Όκ³Ό λ°±λμ°μ΄ λ§λ₯΄κ³ λ³λλ‘'
//1κΈμ μ΄μ μ°μλλ({1, }) [κ°-ν£]μ¬μ΄μ λ¬Έμ(νκΈ)
console.log(str.match(/[κ°-ν£]{1,}/g)) // ['λν΄λ¬Όκ³Ό', 'λ°±λμ°μ΄', 'λ§λ₯΄κ³ ', 'λ³λλ‘']
(\ : λ°±μ¬λμ κΈ°νΈμ λλ€)
/*
\b λ‘ 63κ° λ¬Έμμ ν΄λΉνμ§ μλ λ¬Έμ (λμ΄μ°κΈ°, νΉμλ¬Έμ λ±) λ₯Ό μ λ€λ‘ λ§λκ³ ,
\w{1,} λ‘ 1κΈμ μ΄μ μ°μλλ λͺ¨λ λ¬Έμμ΄μ νμνλ©΄ μλμ κ°μ΄
λ¨μ΄ λ¨μ, νΉμ λ¬Άμλ¨μ(?)μ λ¬Έμ μΆμΆ κ°λ₯
*/
const phone = '010-1234-5678'
const mail = 'www.google.com'
const str = 'The quick brown fox jumps over the lazy dog'
//μ°μλλ λ¬Έμ
console.log(phone.match(/\b\w{1,}\b/g)) //['010', '1234', '5678']
console.log(mail.match(/\b\w{1,}\b/g)) //['www', 'google', 'com']
console.log(str.match(/\b\w{1,}\b/g)) //['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
//tλ‘ μμνλ μ°μλλ λ¬Έμ
console.log(str.match(/\bt\w{1,}\b/gi)) //['The', 'the']
(\ : λ°±μ¬λμ κΈ°νΈμ λλ€)
const str = ` hello
world ! `;
//replace() λ₯Ό νμ©νμ¬ λͺ¨λ 곡백μ "" (λΉλ¬Έμμ΄) λ‘ μ¬λ°°μΉ
console.log(str.replace(/\s/g,"")) //'helloworld!'
const mail = 'abc123@gmail.com'
// @ μμμ€λ 1κΈμμ΄μμ λͺ¨λ λ¬Έμ (μ΄λ©μΌμμ μμ΄λ μΆμΆ)
console.log(mail.match(/.{1,}(?=@)/g)) //['abc123']
const mail = 'abc123@gmail.com'
// @ λ€μμ€λ 1κΈμμ΄μμ λͺ¨λ λ¬Έμ
console.log(mail.match(/(?<=@).{1,}/g)) //['gmail.com']
νμ μ΄λμ κ° μ κ·μ ννμ΄ λ³΄μ΄λ©΄ 머리λΆμ‘κ³ μΌμ.. νλ©° νΌνκΈ° λ°λΉ΄λλ° λ μ΄λ κ² μ°¨κ·Όμ°¨κ·Ό νν€μ³λ³΄λ λμ μ΅κΈ°λ§ νλ©΄ μκ°λ³΄λ€ μ΄λ ΅μ§ μμ κ² κ°κ³ λ λλ¦ νΈνκ³ μ μ©νκ² μ¬μ©ν μ μκ² κ΅¬λ μΆμλ€
κΌ νλ²μ―€ 곡λΆλ₯Ό ν΄μΌκ² λ€ μκ°μ νκ³ μμμ§λ§ λ무 μ΄λ €μ보μ¬μ μλλ λͺ»λ΄κ³ μμλλ° ν¨μΊ μμ λμμ μ΄λ κ² λ‘νλ κ°μλ₯Ό κ°μ Έλ€ λμ μ€ λλΆμ μ΄λ κ² λ 곡λΆκ° λμλ€!!
μ΄λ €μ 보μΈλ€κ³ κ²λ§λ΄λ©΄ μ λ§ λλκ² μμΌλ©°,, λ§μ λΆλͺνλ©΄ λ³ κ±° μλκ²λ€μ΄ λλΆλΆμΈκ²μ λνλ² λλλ€!! λ΄κ° μ΄λ κ² μ»΄ν¨ν°μμμ κ°λ°κ³΅λΆλ₯Ό νκ³ μμκ±°λΌκ³€ μΌλ
μ λ§ ν΄λ κΏλ λͺ»κΎΈλ μΌμΈλ° μ΄λ»κ²λ λΆλͺν보λ μ§κΈ μ΄λ κ² μ΄λ»κ²λ νκ³ μλκ² μ²λΌ!!