Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

SyntaxError: invalid regular expression flag "x"

The JavaScript exception "invalid regular expression flag" occurs when the flags in a regular expression contain any flag that is not one of:d,g,i,m,s,u,v, ory. It may also be raised if the expression contains more than one instance of a valid flag, or when theu andv flags are used together.

Message

SyntaxError: Invalid flags supplied to RegExp constructor 'x' (V8-based)SyntaxError: Invalid regular expression flags (V8-based)SyntaxError: invalid regular expression flag x (Firefox)SyntaxError: Invalid flags supplied to RegExp constructor. (Safari)SyntaxError: Invalid regular expression: invalid flags (Safari)

Error type

What went wrong?

The regular expression contains invalid flags, or valid flags have been used more than once in the expression.

The valid (allowed) flags ared,g,i,m,s,u,v, andy. They are introduced in more detail inRegular expressions > Advanced searching with flags.

Theu andv flags are mutually exclusive, so they cannot be used together. You can read the references to understand what their behavior differences are.

Examples

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:

js
const re = /pattern/flags;

They can also be defined in the constructor function of theRegExp object (second parameter):

js
const re = new RegExp("pattern", "flags");

Here is an example showing use of only correct flags.

js
/foo/g;/foo/gims;/foo/uy;

Below is an example showing the use of some invalid flagsb,a andr:

js
/foo/bar;// SyntaxError: invalid regular expression flag "b"

The code below is incorrect, becauseW,e andb are not valid flags.

js
const obj = {  url: /docs/Web,};// SyntaxError: invalid regular expression flag "W"

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:

js
const obj = {  url: "/docs/Web",};

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp