๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Frontend/JavaScript

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] RegExp(์ •๊ทœํ‘œํ˜„์‹)์˜ ๊ฐœ๋…๊ณผ ์‘์šฉ ์˜ˆ์ œ ์‚ดํŽด๋ณด๊ธฐ

by YWTechIT 2022. 4. 14.
728x90

๐Ÿ“ RegExp(์ •๊ทœํ‘œํ˜„์‹)์˜ ๊ฐœ๋…๊ณผ ์‘์šฉ ์˜ˆ์ œ ์‚ดํŽด๋ณด๊ธฐ

RegExp๋Š” ์ •๊ทœํ‘œํ˜„์‹์ด๋ผ๊ณ  ๋ถ€๋ฅด๋Š”๋ฐ ๋ฌธ์ž์—ด์„ ๋Œ€์ƒ์œผ๋กœ ํŒจํ„ด ๋งค์นญ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ํŒจํ„ด ๋งค์นญ ๊ธฐ๋Šฅ๊ณผ ํŠน์ • ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์ถ”์ถœ ๋˜๋Š” ์น˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ๋งํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๋ฐฉ๋ฉด์—์„œ๋„ ์‚ฌ์šฉ๋˜์ง€๋งŒ ๋ณดํ†ต ๊ธ€์“ด์ด๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๊ฑฐ๋‚˜ ํ”„๋กœ์ ํŠธ ์‹œ util ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์ •๊ทœํ‘œํ˜„์‹์˜ ์žฅ์ ์€ ๋ฐ˜๋ณต๋ฌธ๊ณผ ์กฐ๊ฑด๋ฌธ ์—†์ด ํŒจํ„ด์„ ์ •์˜ํ•˜๊ณ  ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๊ฐ„๋‹จํžˆ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ, ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐํ˜ธ๋ฅผ ํ˜ผํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜์Œ ์ ‘ํ•œ๋‹ค๋ฉด ๊ฐ€๋…์„ฑ์ด ๋งค์šฐ ์ข‹์ง€ ๋ชปํ•œ ๋‹จ์ ์ด ์žˆ๋‹ค. ์ •๊ทœํ‘œํ˜„์‹์—๋Š” ๋‹ค์–‘ํ•œ ํŒจํ„ด์ด ์žˆ์ง€๋งŒ ๊ทธ์ค‘ ๋Œ€ํ‘œ์ ์œผ๋กœ ์“ฐ์ด๋Š” ๊ฒƒ๋“ค๋งŒ ์•Œ์•„๋ณด์ž.

 

๋ณธ๋ก ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ์ •๊ทœ ํ‘œํ˜„์‹์˜ ๊ฒ€์ƒ‰ ๋ฐฉ์‹์„ ์„ค์ •ํ•˜๋Š” ํ”Œ๋ž˜๊ทธ๊ฐ€ ์žˆ๋Š”๋ฐ, ์ด 6๊ฐœ์˜ ํ”Œ๋ž˜๊ทธ๊ฐ€ ์žˆ๋‹ค. ํ”Œ๋ž˜๊ทธ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ ๋„ฃ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค. ๋˜ํ•œ ๊ฒ€์ƒ‰ ๋งค์นญ ๋Œ€์ƒ์ด ์—ฌ๋Ÿฌ ๊ฐœ์—ฌ๋„ ์ฒซ ๋ฒˆ์งธ ๋งค์นญ ๋Œ€์ƒ๋งŒ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์ข…๋ฃŒํ•œ๋‹ค.(์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ ค๋ฉด gํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.)

1. g(global): ํŒจํ„ด๊ณผ ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๊ฐ’๋“ค์„ ์ „์—ญ ๊ฒ€์ƒ‰ํ•œ๋‹ค. (๋ฐ˜์„ฑ๋ฌธ์„ ์˜์–ด๋กœ ํ•ด์„ํ•œ ๊ฒƒ์ด ์•„๋‹˜ ๐Ÿ™ƒ)
2. i(ignoreCase): ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ํŒจํ„ด์„ ๊ฒ€์ƒ‰ํ•œ๋‹ค.
3. m(multiLine): ๋ฌธ์ž์—ด์˜ ํ–‰์ด ๋ฐ”๋€Œ๋”๋ผ๋„ ํŒจํ„ด ๊ฒ€์ƒ‰์„ ์ง€์†ํ•œ๋‹ค.
4. s(source): ๊ฐœํ–‰ ๋ฌธ์ž์ธ `\n`๋„ ํฌํ•จํ•˜๋„๋ก ํ™œ์„ฑํ™” ํ•œ๋‹ค.
5. u(unicode): ์œ ๋‹ˆ์ฝ”๋“œ ์ „์ฒด๋ฅผ ์ง€์›ํ•œ๋‹ค.
6. y(sticky): ๋ฌธ์ž ๋‚ด ํŠน์ • ์œ„์น˜์—์„œ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰ํ•˜๋Š” `sticky` ๋ชจ๋“œ๋ฅผ ํ™œ์„ฑํ™” ์‹œํ‚จ๋‹ค.

 

ํ•˜๋‚˜๋งŒ ๋” ์•Œ์•„๋ณด์ž. ๋ฐ”๋กœ ํŒจํ„ด์ธ๋ฐ, ํ”Œ๋ž˜๊ทธ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹์˜ ๊ฒ€์ƒ‰ ๋ฐฉ์‹์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค๋ฉด, ์ •๊ทœํ‘œํ˜„์‹์˜ ํŒจํ„ด์€ ๋ฌธ์ž์—ด์˜ ์ผ์ •ํ•œ ๊ทœ์น™์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. ํŒจํ„ด์€ ๋ฌธ์ž์—ด์˜ ๋”ฐ์˜ดํ‘œ ๋Œ€์‹  /๋กœ ์—ด๊ณ  ๋‹ซ๋Š”๋‹ค. ์ฆ‰, "Hello"๊ฐ€ ์•„๋‹ˆ๋ผ /Hello/๋กœ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ, ๋”ฐ์˜ดํ‘œ๋ฅผ ํฌํ•จํ•œ๋‹ค๋ฉด ๋”ฐ์˜ดํ‘œ๊นŒ์ง€๋„ ํŒจํ„ด์— ํฌํ•จ๋˜๋‹ˆ ์ด ์ ์— ์ฃผ์˜ํ•˜์ž. ํŒจํ„ด์˜ ์ข…๋ฅ˜๋Š” ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์ง€๋งŒ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. [ ] : ๊ด„ํ˜ธ์•ˆ์— ์žˆ๋Š” ๊ฐ’์€ or ์„ ๋‚˜ํƒ€๋ƒ„
  2. \d: [ 0-9 ]๋ฅผ ์˜๋ฏธํ•จ
  3. \D: ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•จ
  4. \w: ์•ŒํŒŒ๋ฒณ, ์ˆซ์ž, ์–ธ๋”์Šค์ฝ”์–ด๋ฅผ ์˜๋ฏธํ•จ( [ A-Za-z0-9 ])
  5. \W: ์•ŒํŒŒ๋ฒณ, ์ˆซ์ž, ์–ธ๋”์Šค์ฝ”์–ด๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž๋ฅผ ์˜๋ฏธํ•จ
  6. /s: ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ณต๋ฐฑ ๋ฌธ์ž(์ŠคํŽ˜์ด์Šค, ํƒ ๋“ฑ)๋ฅผ ์˜๋ฏธํ•œ๋‹ค. [/t/r/n/v/f] ์™€ ๊ฐ™์€ ์˜๋ฏธ๋‹ค.
  7. *: ์ด์ „ ํ•ญ๋ชฉ์„ 0๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณตํ•จ
  8. +: ์ด์ „ ํ•ญ๋ชฉ์„ 1ํšŒ ์ด์ƒ ๋ฐ˜๋ณตํ•จ
  9. ^: [ ] ๋‚ด๋ถ€์— ์žˆ๋Š” ^ ๋Š” not ์„ ์˜๋ฏธํ•˜๊ณ , [ ] ์™ธ๋ถ€์— ์žˆ๋Š” ^ ๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘์„ ์˜๋ฏธํ•œ๋‹ค.
  10. $: ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰์„ ์˜๋ฏธํ•œ๋‹ค.
  11. ?: ์•ž์„  ํŒจํ„ด์ด ์ตœ๋Œ€ ํ•œ ๋ฒˆ ์ด์ƒ(0๋ฒˆ ํฌํ•จ) ๋ฐ˜๋ณต๋˜๋Š”์ง€๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰, ์•ž์„  ํŒจํ„ด (s)์ด ์žˆ๊ฑฐ๋‚˜ ์—†์–ด๋„ ๋งค์น˜๋œ๋‹ค.
  12. {m,n}: ๋ฐ˜๋ณต ๊ฒ€์ƒ‰ ํŒจํ„ด์œผ๋กœ ์ตœ์†Œ m๋ฒˆ, ์ตœ๋Œ€ n๋ฒˆ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•œ๋‹ค. ์ฝค๋งˆ ๋’ค์— ๊ณต๋ฐฑ์ด ์˜ค์ง€ ์•Š๊ฒŒ ์ฃผ์˜ํ•˜์ž.
728x90

์ž, ์ด์ œ ํ”Œ๋ž˜๊ทธ์™€ ํŒจํ„ด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ดค์œผ๋‹ˆ ๋ณธ๊ฒฉ์ ์œผ๋กœ RegExp ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž. MDN์— ์ ํ˜€์žˆ๋“ฏ์ด ๋ฉ”์„œ๋“œ๋„ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋Š” exec, match, test์ •๋„๋งŒ ์•Œ์•„๋ณด์ž.

 

  1. exec: ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งค์นญ ๊ฒฐ๊ณผ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ null๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํŠน์ด์‚ฌํ•ญ์œผ๋กœ๋Š” g ํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ ์ฒซ ๋ฒˆ์งธ ๋งค์นญ ๊ฒฐ๊ณผ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
let target = "is";
let str = "what is your name? my name is ywtechit";

console.log(/is/g.exec(target));
๐Ÿ‘‰๐Ÿฝ [ 'is', index: 0, input: 'is', groups: undefined ]
  1. match: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ์ผ์น˜ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. exec ๋ฉ”์„œ๋“œ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ g ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋ชจ๋“  ๋งค์นญ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// ex1: ๋ฌธ์ž 'p'์˜ ๊ฐœ์ˆ˜๋ฅผ return ํ•˜์‹œ์˜ค
let lyrics = "i have a pen pineapple apple pen";
let reg = lyrics.match(/p/g) // [ 'p', 'p', 'p', 'p', 'p', 'p', 'p' ]
console.log(reg.length)
๐Ÿ‘‰๐Ÿฝ 7  // ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๊ณง `p`์˜ ๊ฐœ์ˆ˜์™€ ๋™์ผํ•˜๋‹ค.

let target = "what is your name? my name is ywtechit";

// ex2: is ๊ฐ’์„ ํ•œ๋ฒˆ๋งŒ ์ฐพ๊ธฐ
console.log(target.match(/is/));
๐Ÿ‘‰๐Ÿฝ [ 'is', index: 5, input: 'what is your name? my name is ywtechit', groups: undefined ]

// ex3: is ๊ฐ’์„ ๋ชจ๋‘ ์ฐพ๊ธฐ
console.log(target.match(/is/g));
๐Ÿ‘‰๐Ÿฝ [ 'is', 'is' ]

// ex4: A๊ฐ€ 2๋ฒˆ์ด์ƒ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ
const target = "A AA B BB Aa Bb AAA";
const regExp = /A{2,}/g;
const result = target.match(regExp)
console.log('result :>> ', result);  // [ "AA", "AAA" ]

// ex5: URL ์ œ์ผ ๋งˆ์ง€๋ง‰ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜๋Š” ์ •๊ทœํ‘œํ˜„์‹
const url = "https://swtrack.elice.io/courses/16306/lecturerooms/16157";
const reg = url.match(/\/([0-9]+)$/);

console.log(reg);
๐Ÿ‘‰๐Ÿฝ
[
  0: '/16157',
  1: '16157',
  2: index: 51,
  3: input: 'https://swtrack.elice.io/courses/16306/lecturerooms/16157',
  4: groups: undefined
]

// ex6: URL์— ํฌํ•จ๋œ ํŠน์ • ๋ฌธ์ž ๋ถ„๋ฆฌํ•˜๊ธฐ
let reg = s.match(/\/(lecturerooms)\/([0-9]+$)/);
console.log(reg);
๐Ÿ‘‰๐Ÿฝ
[
    0: "/lecturerooms/16157"
    1: "lecturerooms"
    2: "16157"
    groups: undefined
    index: 38
    input: "https://swtrack.elice.io/courses/16306/lecturerooms/16157"
    length: 3
]

// ex7: ๋ฌธ์ž์˜ ๋‚ด์šฉ๊ณผ ์ƒ๊ด€์—†์ด 3์ž๋ฆฌ ๋ฌธ์ž์—ด๊ณผ ๋งค์น˜ํ•˜๊ธฐ
let target = "Is this your mac? Is this your phone?";
console.log(target.match(/.../g));
๐Ÿ‘‰๐Ÿฝ
[
  'Is ', 'thi', 's y',
  'our', ' ma', 'c? ',
  'Is ', 'thi', 's y',
  'our', ' ph', 'one'
]
  1. test: ๋งค์นญ๊ฒฐ๊ณผ๋ฅผ ๋ถˆ๋ฆฌ์–ธ(boolean)๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์ œ์ผ ๋งŽ์ด ์‚ฌ์šฉํ–ˆ๋‹ค. ์—ฌ๋‹ด์œผ๋กœ ์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ ํ˜•์‹์ธ์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๊ณต์‹ ํŒจํ„ด(FC 5322 Official Standard)์€ emailregex.com์—์„œ๋„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
// ex1: ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ธฐ
let target = "what is your name? my name is ywtechit";

console.log(/is/.test(target));  // true

console.log(/typescript/.test(target));  // false

// ex2: new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ RegExp์™€ new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ RegExp
const str = 'table football';
const regex = new RegExp('foo*');
const sameRegex = /foo*/;
const globalRegex = new RegExp('foo*', 'g');

console.log(regex.test(str));  // true

console.log(globalRegex.lastIndex);  // 0

console.log(globalRegex.test(str));  // true

console.log(globalRegex.lastIndex);  // 9

console.log(globalRegex.test(str));  // false

// ex3: ์˜ฌ๋ฐ”๋ฅธ ์ „ํ™”๋ฒˆํ˜ธ ํ˜•์‹์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํŒจํ„ด
const tel = "010-1234-1234"
const regExp = /^\d{3}-\d{4}-\d{4}$/;
const result = regExp.test(tel);
console.log('result :>> ', result);  // true

// ex4: http://, https://๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋„๋ฉ”์ธ์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํŒจํ„ด
const domain = "https://www.naver.com";
const regExp = /^(http|https):\/\//;
const regExp2 = /^https?:\/\//;
const result = regExp.test(domain);
console.log('result :>> ', result);  // true

// ex5: html๋กœ ๋๋‚˜๋Š” ํŒŒ์ผ๋ช…์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํŒจํ„ด
const string = "index.html";
const regExp = /html$/
const result = regExp.test(string)
console.log('result :>> ', result);  // true

// ex6: ์†Œ์ˆ˜์ ์„ ์ตœ๋Œ€ 2๋ฒˆ๊นŒ์ง€๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์ž…๋ ฅ์ธ๊ฐ€?
const isUptoTwoDecimalPoint = (input: string): boolean => {
  const isIncludeDot = /[.]/g;

  if (isIncludeDot.test(input)) {
    const isTwoDecimalPoint = /^\d+[.]\d{1,2}$/;
    if (isTwoDecimalPoint.test(input)) return true;
    return false;
  }
  return true;
};

// ex7: ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ณต๋ฐฑ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š”๊ฐ€?
const string = " Hi!!";
const regExp = /^[\s]+/
const result = regExp.test(string)
console.log('result :>> ', result);  // true

// ex8: ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋๋‚˜๋ฉฐ 4~10์ž๋ฆฌ์ธ๊ฐ€?
const string = "abcd8637";
const regExp = /^[\w]{4,10}$/
const result = regExp.test(string)
console.log('result :>> ', result);  // true

// ex9: ์˜ฌ๋ฐ”๋ฅธ ์ด๋ฉ”์ผ ํ˜•์‹์ธ๊ฐ€?
const string = "ywtechit@gmail.com";
const regExp = /^[\w]([-_\.]?[\w])*@[\w]([-_\.]?[\w])*\.[a-zA-Z]{2,3}$/
const result = regExp.test(string)
console.log('result :>> ', result);  // trueโ€ˆโ€‹โ€‹โ€‹โ€‹โ€‹at

// General Email Regex (RFC 5322 Official Standard)
const regExp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

// HTML5 input ํƒœ๊ทธ์˜ type=”email”์— ์‚ฌ์šฉ๋˜๋Š” ํŒจํ„ด(from W3C: Ref 5.)
<input type="email" placeholder="Enter your email" />
const regExp = /^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

// ex10: ํŠน์ˆ˜๋ฌธ์ž ํŒ๋ณ„ํ•˜๊ธฐ
let s = "(A(BC)D)EF(G(H)(IJ)K)LM(N)";
let answer = "";

console.log(solution(s));

function solution(s) {
  for (let x of s) {
    if (/[A-Z]/.test(x)) answer+=x;
  }
  return answer;
}

๐Ÿ‘‰๐Ÿฝ ABCDEFGHIJKLMN
  1. replace: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด๋‚ด์— ์ผ์น˜๋ฅผ ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด๋กœ ๋Œ€์น˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
// ex1: ๊ณต๋ฐฑ ์ œ๊ฑฐํ•˜๊ธฐ
const str =
  "๋‚˜๋ž๋ง์‹ธ๋ฏธ ๋“•๊ท์— ๋‹ฌ์•„๋ฌธ์ž์™€๋กœ ์„œ๋ฅด ์‚ฌ๋ง›๋”” ์•„๋‹ˆํ• ์Ž„ ์ด๋Ÿฐ ์ „์ฐจ๋กœ ์–ด๋ฆฐ ๋ฐฑ์…ฉ์ด ๋‹ˆ๋ฅด๊ณ ์ ธ ํ™‡๋ฒ ์ด์…”๋„ ๋งˆ์ฐธ๋„ค ์ œ ๋œจ๋“ค ์‹œ๋ŸฌํŽด๋”” ๋ชฏํ•ง ๋…ธ๋ฏธํ•˜๋‹ˆ์•„ ๋‚ด ์ด๋ž„ ์œ™ํ•˜์•ผ ์–ด์—ฟ๋น„๋„ˆ๊ฒจ ์ƒˆ๋กœ ์Šค๋ฏˆ ์—ฌ๋“ซ ์งœ๋ž„ ๋งน๊ฐ€๋…ธ๋‹ˆ์‚ฌ๋žŒ๋งˆ๋‹ค ํ•ด์—ฌ ์ˆ˜๋น„๋‹ˆ๊ฒจ ๋‚ ๋กœ ์‘ค๋ฉ” ๋ป”ํ•œํ‚ˆ ํ•˜๊ณ ์ ธ ํ• ๋”ฐ๋ผ๋ฏธ๋‹ˆ๋ผ";
const result = str.replace(/\s/g, "")

console.log('result :>> ', result);
๐Ÿ‘‰๐Ÿฝ ๋‚˜๋ž๋ง์‹ธ๋ฏธ๋“•๊ท์—๋‹ฌ์•„๋ฌธ์ž์™€๋กœ์„œ๋ฅด์‚ฌ๋ง›๋””์•„๋‹ˆํ• ์Ž„์ด๋Ÿฐ์ „์ฐจ๋กœ์–ด๋ฆฐ๋ฐฑ์…ฉ์ด๋‹ˆ๋ฅด๊ณ ์ ธํ™‡๋ฒ ์ด์…”๋„๋งˆ์ฐธ๋„ค์ œ๋œจ๋“ค์‹œ๋ŸฌํŽด๋””๋ชฏํ•ง๋…ธ๋ฏธํ•˜๋‹ˆ์•„๋‚ด์ด๋ž„์œ™ํ•˜์•ผ์–ด์—ฟ๋น„๋„ˆ๊ฒจ์ƒˆ๋กœ์Šค๋ฏˆ์—ฌ๋“ซ์งœ๋ž„๋งน๊ฐ€๋…ธ๋‹ˆ์‚ฌ๋žŒ๋งˆ๋‹คํ•ด์—ฌ์ˆ˜๋น„๋‹ˆ๊ฒจ๋‚ ๋กœ์‘ค๋ฉ”๋ป”ํ•œํ‚ˆํ•˜๊ณ ์ ธํ• ๋”ฐ๋ผ๋ฏธ๋‹ˆ๋ผ

// ex2: ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐํ•˜๊ธฐ
let s = "found7, time: study; Yduts; emit, 7Dnuof";
let notIncludeSpecialCharacter = s.replace(/[^A-z]/g, '');
๐Ÿ‘‰๐Ÿฝ foundtimestudyYdutsemitDnuof

// ex3: ํŠน์ˆ˜๋ฌธ์ž ์ค‘์—์„œ ๊ณต๋ฐฑ์€ ์‚ด๋ฆฌ๊ณ  ์‹ถ์„ ๋•Œ
let notIncludeSpecialCharacter = s.replace(/[^A-z | " "]/g, '');
๐Ÿ‘‰๐Ÿฝ found time study yduts emit dnuof

์ง€๊ธˆ๊นŒ์ง€ ์ •๊ทœํ‘œํ˜„์‹์˜ ๊ฐœ๋…๊ณผ ์‘์šฉ ์˜ˆ์ œ๋ฅผ ๊ฐ„๋žตํ•˜๊ฒŒ ์‚ดํŽด๋ณด์•˜๋‹ค. ์ฒ˜์Œ ์ •๊ทœํ‘œํ˜„์‹์„ ์ ‘ํ–ˆ์„ ๋• ๋„ํ†ต ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์€ ๋ฌธ๋ฒ•์ด์—ˆ๋Š”๋ฐ, ๋ช‡ ๋ฒˆ ์‚ฌ์šฉํ•ด ๋ฒ„๋ฆ‡ํ•˜๋‹ˆ๊นŒ ๋ฐ˜๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ๋ณด๋‹ค ์ด์šฉํ•˜๊ธฐ๊ฐ€ ์‰ฝ๊ณ  ํŽธํ–ˆ๋‹ค. ์กฐ๊ธˆ์”ฉ ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ฉฐ ๊ฒฝํ—˜์„ ์Œ“๊ณ  ๋‚˜์ค‘์— ํ˜„์—…์— ๋„์ž…ํ•˜๊ฒŒ ๋˜๋ฉด ์˜ˆ์ œ๋ฅผ ๊นŒ๋จน์ง€ ์•Š๋„๋ก ๊ธฐ๋กํ•ด๋‘ฌ์•ผ๊ฒ ๋‹ค.

 

Reference

  1. https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/RegExp
  2. https://ko.javascript.info/regexp-introduction
  3. https://www.aladin.co.kr/shop/wproduct.aspx?ItemId=251552545
  4. https://emailregex.com/
  5. https://html.spec.whatwg.org/multipage/input.html#input.email.attrs.value.multiple
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€