Evanss
Evanss

Reputation: 23563

Uncaught SyntaxError: Invalid regular expression flags - but regex101.com says my regex is OK?

I have a regex which is working according to regex101.com, however when I use it in my JS I get an error:

Uncaught SyntaxError: Invalid regular expression flags

const bandsReduced = bands.map((item)=>{
  return item.replace(/the|a/gAi,'');
});

Upvotes: 1

Views: 1538

Answers (2)

Evanss
Evanss

Reputation: 23563

The ^ character is used in JavaScript to limit the search to the beginning of the string:

const bandsReduced = bands.map((item)=>{
  return item.replace(/^the|^a/ig,'');
});

Upvotes: 1

Alicia Sykes
Alicia Sykes

Reputation: 7077

A is not a flag in JavaScript.

Valid flags are g,i, m, u, y. See the documentation here: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions

Different languages have slightly different regex syntax, in regex101 you can change the language as per image below.

enter image description here

(as mentioned by elcanrs in the comment)

Upvotes: 2

Related Questions