In a regular expression literal, which consists of a pattern enclosed between slashes, the flags are defined after the second slash. Regular expression flags can be used separately or together in any order. This syntax shows how to declare the flags using the regular expression literal:
const re = /pattern/flags;
They can also be defined in the constructor function of the RegExp
object (second parameter):
const re = new RegExp("pattern", "flags");
Here is an example showing use of only correct flags.
/foo/g;
/foo/gims;
/foo/uy;
Below is an example showing the use of some invalid flags b
, a
and r
:
The code below is incorrect, because W
, e
and b
are not valid flags.
const obj = {
url: /docs/Web,
};
An expression containing two slashes is interpreted as a regular expression literal. Most likely the intent was to create a string literal, using single or double quotes as shown below:
const obj = {
url: "/docs/Web",
};