SyntaxError: invalid regular expression flag "x"

๋ฉ”์„ธ์ง€

SyntaxError: Syntax error in regular expression (Edge)
SyntaxError: invalid regular expression flag "x" (Firefox)
SyntaxError: Invalid regular expression flags (Chrome)

์—๋Ÿฌ ํƒ€์ž…

SyntaxError

๋ฌด์—‡์ด ์ž˜๋ชป๋˜์—ˆ์„๊นŒ?

์ฝ”๋“œ์— ์ž˜๋ชป๋œ ์ •๊ทœ ํ‘œํ˜„์‹ ํ”Œ๋ž˜๊ทธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์Šฌ๋ž˜์‹œ๋กœ ๋ฌถ์ธ ํŒจํ„ด์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ์ž์—์„œ ํ”Œ๋ž˜๊ทธ๋Š” ๋‘ ๋ฒˆ์งธ ํ”Œ๋ž˜๊ทธ ๋’ค์— ์ •์˜๋ฉ๋‹ˆ๋‹ค. ํ”Œ๋ž˜๊ทธ๋Š” ๋˜ํ•œ RegExp ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ๋„ ์ •์˜๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜). ์ •๊ทœ ํ‘œํ˜„์‹ ํ”Œ๋ž˜๊ทธ๋Š” ๋”ฐ๋กœ ๋˜๋Š” ์ˆœ์„œ์— ์ƒ๊ด€ ์—†์ด ๊ฐ™์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์ง€๋งŒ ECMAScript์—๋Š” ์˜ค์ง 5๊ฐœ๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค.

์ •๊ทœ ํ‘œํ˜„์‹์— ํ”Œ๋ž˜๊ทธ๋ฅผ ํฌํ•จ์‹œํ‚ค๋ ค๋ฉด ์•„๋ž˜์˜ ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜์„ธ์š”:

var re = /pattern/flags;

๋˜๋Š”

var re = new RegExp('pattern', 'flags');
์ •๊ทœ ํ‘œํ˜„์‹ ํ”Œ๋ž˜๊ทธ
ํ”Œ๋ž˜๊ทธ ์„ค๋ช…
g ์ „์—ญ ๊ฒ€์ƒ‰.
i ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„ ์—†์ด ๊ฒ€์ƒ‰.
m ์—ฌ๋Ÿฌ ์ค„(Multi-line) ๊ฒ€์ƒ‰.
u ์œ ๋‹ˆ์ฝ”๋“œ; ํŒจํ„ด์„ ์œ ๋‹ˆ์ฝ”๋“œ ์ฝ”๋“œ ํฌ์ธํŠธ์˜ ๋‚˜์—ด๋กœ ์ทจ๊ธ‰.
y ํ˜„์žฌ ์œ„์น˜์—์„œ ๊ฒ€์ƒ‰. ("sticky" ๊ฒ€์ƒ‰). sticky (en-US) ์ฐธ์กฐ.

์˜ˆ์ œ

5๊ฐœ์˜ ํ”Œ๋ž˜๊ทธ๋งŒ์ด ์œ ํšจํ•ฉ๋‹ˆ๋‹ค.

/foo/bar;

// SyntaxError: invalid regular expression flag "b"

์ •๊ทœ ํ‘œํ˜„์‹์„ ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ๋‚˜์š”? ๋‘ ๊ฐœ์˜ ์Šฌ๋ž˜์‹œ๊ฐ€ ํฌํ•จ๋œ ํ‘œํ˜„์‹์€ ์ •๊ทœ ํ‘œํ˜„์‹ ๋ฌธ์ž๋กœ ํ•ด์„๋ฉ๋‹ˆ๋‹ค.

let obj = {
  url: /docs/Web
};

// SyntaxError: invalid regular expression flag "W"

์•„๋‹ˆ๋ฉด ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ๋‚˜์š”? ์ž‘์€ ๋”ฐ์˜ดํ‘œ ๋˜๋Š” ํฐ ๋”ฐ์˜ดํ‘œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

let obj = {
  url: '/docs/Web'
};

์œ ํšจํ•œ ์ •๊ทœ ํ‘œํ˜„์‹ ํ”Œ๋ž˜๊ทธ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ—ˆ์šฉํ•˜๋Š” 5๊ฐœ์˜ ์œ ํšจํ•œ ์ •๊ทœ ํ‘œํ˜„์‹ ํ”Œ๋ž˜๊ทธ๋ฅผ ์œ„์˜ ํ‘œ์—์„œ ํ™•์ธํ•˜์„ธ์š”.

/foo/g;
/foo/gim;
/foo/uy;

๊ฐ™์ด ๋ณด๊ธฐ

  • ์ •๊ทœ ํ‘œํ˜„์‹
  • XRegEx flags โ€“ ์ƒˆ๋กœ์šด 4๊ฐœ์˜ ํ”Œ๋ž˜๊ทธ(n, s, x, A)๋ฅผ ์ œ๊ณตํ•˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ