JavaScript SyntaxError – Invalid regular expression flag “x”

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.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
        var patt = /GeeksforGeeks/i;
        var str = 'This is GeeksforGeeks';
        document.write(str.match(patt));
    </script>
</body>
</html>

chevron_right


Output:

GeeksForGeeks

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

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Syntax Error</title>
</head>
<body>
    <script>
        var patt = /Geek/GFG;
        var str = 'This is GeeksforGeeks';
        document.write(str.match(patt));
    </script>
</body>
</html>

chevron_right


Output(in console):

SyntaxError: Invalid regular expression flags

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.