Open In App

JavaScript SyntaxError – Invalid regular expression flag “x”

Improve
Improve
Like Article
Like
Save
Share
Report

This JavaScript exception invalid regular expression flag occurs if the flags, written after the second slash in RegExp literal, are not from either of (g, i, m, s, u, or y).

Message:

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

Error Type:

SyntaxError

Cause for Error: Somewhere in the code, There are regular expression flags that are not valid.

Example 1: This example has valid RegExp flags.

Javascript




let patt = /GeeksforGeeks/i;
let str = 'This is GeeksforGeeks';
console.log(str.match(patt));


Output:

GeeksForGeeks

Example 2: In this example, After the second slash ‘GFG’ is used as a flag that is invalid.

Javascript




let patt = /Geek/GFG;
let str = 'This is GeeksforGeeks';
console.log(str.match(patt));


Output(in console):

SyntaxError: Invalid regular expression flags

Last Updated : 22 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads