1

Here is my regex:

var newRegexp= new RegExp('\[\/\[\]\^\$\|\*\+\(\)\\~@#%&\-_+=\{}£<>]{1,}', 'g');

I get the Uncaught SyntaxError: Invalid regular expression: Nothing to repeat error with it, and I can't figure out why. I used it on RegExr and it works fine. Any help is appreciated.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
mattz330
  • 59
  • 2
  • 13

2 Answers2

3

You have an escaped first [ that is supposed to start a range. Unescape it.

Also mind that it is better to use a literal notation (I also suggest removing unnecessary escaping as it makes the regex too unreadable):

var newRegexp = /[\/\[\]^$|*+()\\~@#%&_+={}£<>-]+/g;

See demo

Also note that I replaced {1,} with + as it is shorter and means absolutely the same. If we put the hyphen at the end of the character class, we do not have to escape it.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1

You need to double escape the \ like, also inside [] there is no need escape most of the regex special characters

new RegExp('[/\\[\\]^$|*+()~@#%&\\-_+={}£<>]{1,}', 'g');
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531