W3cubDocs

/JavaScript

Errors: Bad regexp flag

Message

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

Error type

SyntaxError

What went wrong?

There are invalid regular expression flags in the code. In a regular expression literal, which consists of a pattern enclosed between slashes, the flags are defined after the second flag. They can also be defined in the constructor function of the RegExp object (second parameter). Regular expression flags can be used separately or together in any order, but there are only five of them in ECMAScript.

To include a flag with the regular expression, use this syntax:

var re = /pattern/flags;

or

var re = new RegExp('pattern', 'flags');
Regular expression flags
Flag Description
g Global search.
i Case-insensitive search.
m Multi-line search.
u Unicode; treat pattern as a sequence of Unicode code points
y Perform a "sticky" search that matches starting at the current position in the target string. See sticky

Examples

There are only five valid regular expression flags.

/foo/bar;

// SyntaxError: invalid regular expression flag "b"

Did you intend to create a regular expression? An expression containing two slashes is interpreted as a regular expression literal.

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

// SyntaxError: invalid regular expression flag "W"

Or did you mean to create a string instead? Add single or double quotes to create a string literal.

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

Valid regular expression flags

See the table above for the five valid regular expression flags that are allowed in JavaScript.

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

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_regexp_flag