1. RegExp ๐Ÿ‘ฉโ€๐Ÿ’ป

1. Features of the RegExp

์ •๊ทœํ‘œํ˜„์‹์˜ ์ฃผ์š” ๊ธฐ๋Šฅ์ด๋‹ค.

  • ๋ฌธ์ž ๊ฒ€์ƒ‰(Search)
  • ๋ฌธ์ž ์ถ”์ถœ(Extract)
  • ๋ฌธ์ž ๋Œ€์น˜(Replace)

์‚ฌ์‹ค ํšŒ์‚ฌ์—์„œ ์ผํ•˜๋ฉฐ ์ •๊ทœํ‘œํ˜„์‹์„ ์ผ๋˜ ๊ฑด ๊ฑฐ์˜ Validation Check ๋ง๊ณค ์—†์—ˆ๋˜ ๊ฒƒ ๊ฐ™๊ธด ํ•œ๋ฐ ๋งค๋ฒˆ https://regexr.com ์— ์˜์กดํ•ด์„œ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค. ์—ญ์‹œ ๊ณ„์† ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ์ข‹์ง€๋งŒ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•ด์„œ ๊ธ€๋กœ ๊ธฐ๋กํ•˜๋Š” ๊ฒƒ ๋งŒํผ ๋จธ๋ฆฟ์†๋„ ์ •๋ฆฌ๋˜๊ณ , ๋‚˜์ค‘์— ๋‹ค์‹œ ์ฐพ์•„๋ณด๊ธฐ ์ข‹์€ ๊ฒƒ์€ ์—†๋Š” ๊ฒƒ ๊ฐ™์•„ ๊ธ€๋กœ ์ž‘์„ฑํ•ด๋ณด๊ณ ์žํ•œ๋‹ค.

์ด ๊ธ€์—์„œ ์‚ฌ์šฉํ•  ๋ฌธ์ž์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

const str = `
010-1234-5678
thesecon@gmail.com
Hello world!
https://www.omdbapi.com/?apikey=7035c60c&s=frozen
The quick brown fox jumps over the lazy dog.
hello@naver.com
http://localhost:1234
๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก
abbcccddddeeeee
`;

2. Features of the RegExp

__ 1) Constructors__

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

__ 2) Literals__

// /ํ‘œํ˜„/์˜ต์…˜
/[a-z]/gi

์ƒ์„ฑ์ž ๋ฐฉ์‹์œผ๋กœ๋งŒ ์œ ํšจํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํŒจํ„ด์ด ์กด์žฌํ•  ๋•Œ๋ฅผ ์ œ์™ธํ•˜๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

3. Methods

์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • regexp.test(string:) -> boolean: ๋ฌธ์ž ๊ฒ€์ƒ‰(Search)
  • string.match(regexp:) -> string[]: ๋ฌธ์ž ์ถ”์ถœ(Extract) - ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์˜ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
  • string.replace(regexp:,replacement:): ๋ฌธ์ž ๋Œ€์น˜(Replace)
console.log(regexp.test(str)); // true
console.log(str.match(regexp)); // ['fox']
console.log(str.replace(regexp, 'cat'));

test๋Š” ์ •๊ทœ์‹์˜ ๋ฉ”์„œ๋“œ๊ณ , match์™€ replace๋Š” string ์˜ ๋ฉ”์„œ๋“œ๋‹ค.

4. Options(=Flags)

  • g: ๋ชจ๋“  ๋ฌธ์ž ์ผ์น˜(Global)
  • i: ๋Œ€์†Œ๋ฌธ์ž ๋ฌด์‹œ(Ignore Case)
  • m: Multi Line, ์ค„๋ฐ”๊ฟˆ์„ ๊ฐ๊ฐ์˜ ๋ฌธ์ž์—ด๋กœ ์ธ์‹
console.log(str.match(/the/));
[
  0: 'the', 
  index: 15, 
  input: '\n010-1234-5678\nthesecon@gmail.com\nHello world!\nhttโ€ฆocalhost:1234\n๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก\nabbcccddddeeeee\n', 
  groups: undefined
]

๊ฐ€์žฅ ์•ž์— ๋‚˜์˜ค๋Š” the๋ฅผ ํ•˜๋‚˜๋งŒ ์ฐพ๋Š”๋‹ค.

console.log(str.match(/the/g));   // ['the', 'the']
console.log(str.match(/the/gi));  // ['the', 'The', 'the']

the๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ฐพ๊ณ , the๋ฅผ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ์—ฌ๋Ÿฌ ๊ฐœ ์ฐพ๋Š”๋‹ค.

console.log(str.match(/\.$/gi));  // null

.์€ ์ •๊ทœ์‹์—์„œ ๊ธฐ๋Šฅ์„ ๊ฐ–๊ณ  ์žˆ์–ด ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด Escape Character \๋ฅผ ์•ž์— ๋ถ™์—ฌ์ฃผ์—ˆ๋‹ค. $๋Š” ๋ฌธ์žฅ์ด ๋๋‚จ์„ ์˜๋ฏธํ•œ๋‹ค.

console.log(str.match(/\.$/gim));  // ['.']

m์ด ํฌํ•จ๋˜์„œ ๋ฌธ์ž์—ด ์ „์ฒด๊ฐ€ ์•„๋‹Œ ๋ผ์ธ ๋‹จ์œ„๋กœ ๊ฒ€์‚ฌํ•ด .๋กœ ๋๋‚˜๋Š” ๋ฌธ์ž์—ด์„ ์ฐพ์•„ ์ถ”์ถœํ–ˆ๋‹ค.


2. Patterns ๐Ÿ‘ฉโ€๐Ÿ’ป

const str = `
010-1234-5678
thesecon@gmail.com
Hello world!
https://www.omdbapi.com/?apikey=7035c60c&s=frozen
The quick brown fox jumps over the lazy dog.
hello@naver.com
http://localhost:1234
๋™ํ•ด๋ฌผ๊ณผ ๋ฐฑ๋‘์‚ฐ์ด ๋งˆ๋ฅด๊ณ  ๋‹ณ๋„๋ก
abbcccddddeeeee
`;

๋ฅผ ์ด์–ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

1. Basic Patterns

  • ^ab: Line ์‹œ์ž‘์— ์žˆ๋Š” ab ์™€ ์ผ์น˜.
  • ab$: Line ๋์— ์žˆ๋Š” ab ์™€ ์ผ์น˜.
  • .: ์ž„์˜์˜ ํ•œ ๋ฌธ์ž์™€ ์ผ์น˜.
  • a|b: a ๋˜๋Š” b ์™€ ์ผ์น˜.
  • ab?: ? ๋ฐ”๋กœ ์ง์ „์˜ ๋ฌธ์ž๋ฅผ optional ๋กœ ์ผ์น˜.
console.log(str.match(/^h.../gm));      // ['http', 'hell', 'http']
console.log(str.match(/\.com$/gm));     // ['.com', '.com']
console.log(str.match(/...\.com$/gm));  // ['ail.com', 'ver.com']
console.log(str.match(/fox|dog/g));     // ['fox', 'dog']
console.log(str.match(/https?/g));      // ['https', 'http']

/https?/g๋Š” /https|http/g์™€ ๊ฐ™๋‹ค. ๋‹จ, /http|https/g๋Š” ์ž˜๋ชป๋œ ํ‘œํ˜„์‹์ž„์— ์œ ์˜ํ•œ๋‹ค. ์„ ํ–‰ ์กฐ๊ฑด์˜ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ๋จผ์ € ๊ฒ€์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— http๋งŒ ์ฐพ๊ณ  ์ผ์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— https๊นŒ์ง€ ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ์กฐ๊ฑด์— ํ•ด๋‹นํ•œ๋‹ค.

console.log(str.match(/https?/g));      // ['https', 'http']
console.log(str.match(/https|http/g));  // ['https', 'http']
console.log(str.match(/http|https/g));  // ['http', 'http']

2. Range Patterns

๋ฒ”์œ„ ํŒจํ„ด์€ [ ]๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • [abe]: a ๋˜๋Š” b ๋˜๋Š” e ์™€ ์ผ์น˜.
  • [a-z]: a ๋ถ€ํ„ฐ z ๊นŒ์ง€ ์ผ์น˜.
  • [A-Z]: A ๋ถ€ํ„ฐ Z ๊นŒ์ง€ ์ผ์น˜.
  • [0-9]: 0 ๋ถ€ํ„ฐ 9 ๊นŒ์ง€ ์ผ์น˜.
  • [๊ฐ€-ํžฃ]: ๊ฐ€ ๋ถ€ํ„ฐ ํžฃ ๊นŒ์ง€ ์ผ์น˜.
console.log(str.match(/[fgj]/g)); // ['g', 'f', 'f', 'j', 'g']

[ ] ์•ˆ์— -๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š์œผ๋ฉด ๊ฐ ๋ฌธ์ž๋ฅผ |๋กœ ์ผ์น˜ ์—ฌ๋ถ€๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋”ฐ๋ผ์„œ [fgj]๋Š” f|g|j์™€ ๊ฐ™๋‹ค.

console.log(str.match(/[0-9]/g)); // ['0', '1', '0', '1', '2', '3', '4', '5', '6', '7', 
                                  // '8', '7', '0', '3', '5', '6', '0', '1', '2', '3', '4']
console.log(str.match(/[๊ฐ€-ํžฃ]/g));   // ['๋™', 'ํ•ด', '๋ฌผ', '๊ณผ', '๋ฐฑ', '๋‘', '์‚ฐ', '์ด', 
                                     // '๋งˆ', '๋ฅด', '๊ณ ', '๋‹ณ', '๋„', '๋ก']
console.log(str.match(/[๊ฐ€-ํžฃ]+/g));  // ['๋™ํ•ด๋ฌผ๊ณผ', '๋ฐฑ๋‘์‚ฐ์ด', '๋งˆ๋ฅด๊ณ ', '๋‹ณ๋„๋ก']

+ ๊ธฐํ˜ธ๋Š” ๋ฐ”๋กœ ๋‹ค์Œ Repeat Patterns ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

3. Repeat Patterns

๋ฐ˜๋ณต ํŒจํ„ด์€ { } ๋˜๋Š” ํŠน์ˆ˜ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ–๋Š” +๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • {m}: mํšŒ ์—ฐ์† ์ผ์น˜
  • {m,}: mํšŒ ์ด์ƒ ์—ฐ์† ์ผ์น˜
  • {m,n}: mํšŒ ์ด์ƒ nํšŒ ์ดํ•˜ ์—ฐ์† ์ผ์น˜
  • +: 1ํšŒ ์ด์ƒ ์—ฐ์† ์ผ์น˜
console.log(str.match(/\d{3}/g));   // ['010', '123', '567', '703', '123']
console.log(str.match(/\d{3,}/g));  // ['010', '1234', '5678', '7035', '1234']
console.log(str.match(/\d{2,4}/g)); // ['010', '1234', '5678', '7035', '60', '1234']
console.log(str.match(/\d+/g));     // ['010', '1234', '5678', '7035', '60', '1234']

\d๋Š” ๋ฐ”๋กœ ๋‹ค์Œ Escape Patterns ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

console.log(str.match(/e{3}/g));    // ['eee']
console.log(str.match(/[a-z]+/g));  // ['thesecon', 'gmail', 'com', 'ello', 'world', 'https', 
                                    // 'www', 'omdbapi', 'com', 'apikey', 'c', 'c', 's', 'frozen', 
                                    // 'he', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 
                                    // 'lazy', 'dog', 'hello', 'naver', 'com', 'http', 'localhost', 
                                    // 'abbcccddddeeeee']

+๋Š” {1,}์™€ ๊ฐ™๋‹ค.

์ด๋ฅผ ์‘์šฉํ•˜๋ฉด ์ž„์˜์˜ ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ชจ๋“  ๋ฌธ์ž ์ผ์น˜๋Š” .{1,} ๋˜๋Š” .+๊ฐ€ ๋˜๊ณ , 0๊ฐœ ์ด์ƒ์˜ ๋ชจ๋“  ๋ฌธ์ž ์ผ์น˜๋Š” .{0,} ๋˜๋Š” .*๊ฐ€ ๋œ๋‹ค.

4. Escape Patterns

  • \w: 63๊ฐœ ๋ฌธ์ž(๋Œ€๋ฌธ์ž(26) + ์†Œ๋ฌธ์ž(26) + ์ˆซ์ž(10) + _(1) ์ด 63๊ฐœ)์™€ ์ผ์น˜, [a-zA-Z0-9_]์™€ ๊ฐ™๋‹ค.
  • \b: 63๊ฐœ ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜๋Š” ์•Š๋Š” ๋ฌธ์ž ๊ฒฝ๊ณ„(Boundary).
  • \d: ์ˆซ์ž(Digit)์™€ ์ผ์น˜, [0-9]์™€ ๊ฐ™๋‹ค.
  • \s: ๊ณต๋ฐฑ(Space, Tab, Newline ๋“ฑ)๊ณผ ์ผ์น˜.
console.log(str.match(/[0-9]/g)); // ['0', '1', '0', '1', '2', '3', '4', '5', '6', '7', 
                                  // '8', '7', '0', '3', '5', '6', '0', '1', '2', '3', '4']
console.log(str.match(/\d/g));    // ['0', '1', '0', '1', '2', '3', '4', '5', '6', '7', 
                                  // '8', '7', '0', '3', '5', '6', '0', '1', '2', '3', '4']
console.log(str.match(/\w+/g));   // ['010', '1234', '5678', 'thesecon', 'gmail', 'com', 
                                  // 'Hello', 'world', 'https', 'www', 'omdbapi', 'com', 
                                  // 'apikey', '7035c60c', 's', 'frozen', 'The', 'quick', 
                                  // 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog', 
                                  // 'hello', 'naver', 'com', 'http', 'localhost', '1234', 
                                  // 'abbcccddddeeeee']

_๋ฅผ ํฌํ•จํ•ด ๋ชจ๋“  ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž์™€ ์ˆซ์ž๋ฅผ ์ถ”์ถœํ•œ๋‹ค.

console.log(str.match(/\s/g));  // ['\n', '\n', '\n', ' ', '\n', '\n', ' ', ' ', ' ', ' ', 
                                // ' ', ' ', ' ', ' ', '\n', '\n', ' ', '\n', ' ', ' ', ' ', 
                                // '\n', '\n']


const str = `
a111a
#222a
a333#
#444#
010-1234-5678
`;

console.log(str.match(/[0-9]+/g));      // ['111', '222', '333', '444', '010', '1234', '5678']
console.log(str.match(/\b[0-9]+/g));    // ['222', '444', '010', '1234', '5678']
console.log(str.match(/[0-9]+\b/g));    // ['333', '444', '010', '1234', '5678']
console.log(str.match(/\b[0-9]+\b/g));  // ['444', '010', '1234', '5678']

\b๋Š” ๋‹จ๋…์œผ๋กœ ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ  ์œ„์™€ ๊ฐ™์ด ๋‹ค๋ฅธ ํŒจํ„ด๊ณผ ๊ฒฐํ•ฉํ•ด Boundary๋ฅผ ๊ฒ€์‚ฌํ•˜๊ธฐ ์œ„ํ•œ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ๋งค์นญ์€ ๋ชจ๋“  ์—ฐ์†๋œ ์ˆซ์ž๋ฅผ ์ถ”์ถœํ–ˆ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ๋ฏธ์นญ์€ word ๊ฐ€ ์•„๋‹Œ ๊ฒƒ ๋’ค์— ์—ฐ์†๋œ ์ˆซ์ž ์กฐํ•จ์„ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— ์•ž์— ๋ฌธ์ž a ๊ฐ€ ๋ถ™์€ ์ˆซ์ž 111๊ณผ 333์€ ์ œ์™ธ๋˜์—ˆ๋‹ค.
  • ์„ธ ๋ฒˆ์งธ ๋งค์นญ์€ ๋ฐ˜๋Œ€๋กœ ๋’ค์— word ๊ฐ€ ์•„๋‹Œ ๊ฒƒ์„ ์ฐพ์œผ๋ฏ€๋กœ, ๋’ค์— ๋ฌธ์ž a ๊ฐ€ ๋ญ์€ ์ˆซ์ž 222์™€ 444๋Š” ์ œ์™ธ๋˜์—ˆ๋‹ค.
  • ๋„ค ๋ฒˆ์งธ ๋งค์นญ์€ ์•ž๋’ค๊ฐ€ ๋ชจ๋‘ word ๊ฐ€ ์•„๋‹Œ ๊ฒƒ์„ ์ฐพ์œผ๋ฏ€๋กœ, ์ˆซ์ž 444, 010, 1234, 5678 ๋งŒ ์ถ”์ถœํ–ˆ๋‹ค.

5. Group Patterns

  • (?:): ๊ทธ๋ฃน ์ผ์น˜
  • (?=): ๊ทธ๋ฃน ์•ž์ชฝ ํƒ์ƒ‰(Lookahead), ๋’ท์ชฝ์— Boundary ๋ฅผ ์„ค์ •ํ•ด ์•ž์ชฝ ์ผ์น˜๋ฅผ ํƒ์ƒ‰
  • (?<=): ๊ทธ๋ฃน ๋’ท์ชฝ ํƒ์ƒ‰(Lookbehind), ์•ž์ชฝ์— Boundary ๋ฅผ ์„ค์ •ํ•ด ๋’ท์ชฝ ์ผ์น˜๋ฅผ ํƒ‘์ƒ‰


1 ) (?:): ๊ทธ๋ฃน ์ผ์น˜

http ๋˜๋Š” https๋ฅผ ํฌํ•จํ•ด ๋„๋ฉ”์ธ์„ ์ถ”์ถœํ•˜๊ณ  ์‹ถ๋‹ค๊ณ  ํ•ด๋ณด์ž.

const str = `
https://www.omdbapi.com?apikey=7035c60c&s=frozen
http://localhost:1234
https://www.amazon.com/gp/bestsellers/?ref_=nav_em_cs_bestsellers_0_1_1_2
`;

console.log(str.match(/https?:\/\/\w+\.?/g));
// ['https://www.', 'http://localhost', 'https://www.']

์ด์ œ \w+\.? ์ด ๋ถ€๋ถ„์„ ๋ฐ˜๋ณตํ•  ๊ฒƒ์ด๋‹ค.

console.log(str.match(/https?:\/\/\w+\.?\w+\.?/g));
// ['https://www.omdbapi.', 'http://localhost', 'https://www.amazon.']
console.log(str.match(/https?:\/\/\w+\.?\w+\.?\w+\.?/g));
// ['https://www.omdbapi.com', 'http://localhost', 'https://www.amazon.com']

๋„๋ฉ”์ธ ๋งˆ์ง€๋ง‰์— /๋กœ ๋๋‚  ๊ฒฝ์šฐ ์ด๊ฒƒ๋„ ํฌํ•จ์‹œํ‚ค๊ณ  ์‹ถ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  \/?๋ฅผ ๋งˆ์ง€๋ง‰์— ์ถ”๊ฐ€ํ•ด๋ณด์ž.

console.log(str.match(/https?:\/\/\w+\.?\w+\.?\w+\.?\/?/g));
// ['https://www.omdbapi.com', 'http://localhost', 'https://www.amazon.com/']

์œ„ ํŒจํ„ด์˜ ๋ฌธ์ œ๋Š” ๋™์ผ ํŒจํ„ด์ด ๋ฐ˜๋ณต๋˜๋Š” ๋ฐ ๊ณ„์†ํ•ด์„œ ํ•˜๋“œ์ฝ”๋”ฉ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ๋ฐ˜๋ณต๋˜๋Š” ํŒจํ„ด์˜ ํšŸ์ˆ˜๊ฐ€ ๊ณ ์ •์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

console.log(str.match(/https?:\/\/(?:\w+\.?)+\/?/g));
// ['https://www.omdbapi.com', 'http://localhost', 'https://www.amazon.com/']

๋ฐ˜๋ณต๋˜๋Š” ํŒจํ„ด \w+\.?์„ (?:\w+\.?)+๋กœ ๊ทธ๋ฃนํ™” ํ•˜๊ณ , ๋’ค์— \/?๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ์–ด๋–ค ๊ฒฝ์šฐ์—๋“  ์ž‘๋™์ด ๊ฐ€๋Šฅํ•˜๋‹ค.


2 ) (?=): ๊ทธ๋ฃน ์•ž์ชฝ ์ผ์น˜(Lookahead)

\b์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Boundary ๊ฐœ๋…์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

const str = `
์•„๋ชฌ๋“œ์šฐ์œ 
์•„๋ชฌ๋“œ๋นต
์•„๋ชฌ๋“œ์ƒ๋Ÿฌ๋“œ
๋ฐ”๋‚˜๋‚˜์šฐ์œ 
๋ฐ”๋‚˜๋‚˜๋นต
๊ณ ๊ตฌ๋งˆ์šฐ์œ 
๊ณ ๊ตฌ๋งˆ๋นต
๊ณ ๊ตฌ๋งˆํŠ€๊น€
๊ณ ๊ตฌ๋งˆํ”ผ์ž
`;
console.log(str.match(/.+(?:์šฐ์œ )/g));  // ['์•„๋ชฌ๋“œ์šฐ์œ ', '๋ฐ”๋‚˜๋‚˜์šฐ์œ ', '๊ณ ๊ตฌ๋งˆ์šฐ์œ ']
console.log(str.match(/.+(?=์šฐ์œ )/g));  // ['์•„๋ชฌ๋“œ', '๋ฐ”๋‚˜๋‚˜', '๊ณ ๊ตฌ๋งˆ']

(?:): ๊ทธ๋ฃน ์ผ์น˜๋Š” ์šฐ์œ ๋ฅผ ํฌํ•จํ•ด โ€˜์•„๋ชฌ๋“œ์šฐ์œ โ€™, โ€˜๋ฐ”๋‚˜๋‚˜์šฐ์œ โ€™, โ€˜๊ณ ๊ตฌ๋งˆ์šฐ์œ โ€™๋ฅผ ์ถ”์ถœํ•˜์ง€๋งŒ,
(?=): ๊ทธ๋ฃน ์•ž์ชฝ ํƒ์ƒ‰(Lookahead)์€ ์šฐ์œ ๋ฅผ ๋’ท์ชฝ Boundary ๋กœ ์„ค์ •ํ•ด ์šฐ์œ ๋ฅผ ์ œ์™ธํ•œ ์•ž์ชฝ์˜ ์ผ์น˜ ๋ฌธ์ž์—ด โ€˜์•„๋ชฌ๋“œโ€™, โ€˜๋ฐ”๋‚˜๋‚˜โ€™, โ€˜๊ณ ๊ตฌ๋งˆโ€™๋งŒ ์ถ”์ถœํ•œ๋‹ค.

3 ) (?<=): ๊ทธ๋ฃน ๋’ค์ชฝ ์ผ์น˜(Lookbehind)

console.log(str.match(/(?:๊ณ ๊ตฌ๋งˆ).+/g));  // ['๊ณ ๊ตฌ๋งˆ์šฐ์œ ', '๊ณ ๊ตฌ๋งˆ๋นต', '๊ณ ๊ตฌ๋งˆํŠ€๊น€', '๊ณ ๊ตฌ๋งˆํ”ผ์ž']
console.log(str.match(/(?<=๊ณ ๊ตฌ๋งˆ).+/g)); // ['์šฐ์œ ', '๋นต', 'ํŠ€๊น€', 'ํ”ผ์ž']

(?:): ๊ทธ๋ฃน ์ผ์น˜๋Š” ๊ณ ๊ตฌ๋งˆ๋ฅผ ํฌํ•จํ•ด โ€˜๊ณ ๊ตฌ๋งˆ์šฐ์œ โ€™, โ€˜๊ณ ๊ตฌ๋งˆ๋นตโ€™, โ€˜๊ณ ๊ตฌ๋งˆํŠ€๊น€โ€™, โ€˜๊ณ ๊ตฌ๋งˆํ”ผ์žโ€™๋ฅผ ์ถ”์ถœํ•˜์ง€๋งŒ,
(?<=): ๊ทธ๋ฃน ๋’ท์ชฝ ํƒ์ƒ‰(Lookbehind)์€ ๊ณ ๊ตฌ๋งˆ๋ฅผ ์•ž์ชฝ Boundary ๋กœ ์„ค์ •ํ•ด ๊ณ ๊ตฌ๋งˆ๋ฅผ ์ œ์™ธํ•œ ๋’ท์ชฝ์˜ ์ผ์น˜ ๋ฌธ์ž์—ด โ€˜์šฐ์œ โ€™, โ€˜๋นตโ€™, โ€˜ํŠ€๊น€โ€™, โ€˜ํ”ผ์žโ€™๋งŒ ์ถ”์ถœํ•œ๋‹ค.




Reference

  1. ๋ฐ•์˜์›…, โ€œํ”„๋ก ํŠธ์—”๋“œ ์›น ๊ฐœ๋ฐœ์˜ ๋ชจ๋“  ๊ฒƒ ์ดˆ๊ฒฉ์ฐจ ํŒจํ‚ค์ง€ Online.โ€ fastcampus.co.kr. last modified unknown, Fast Campus.