Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  86] [ 5]  / answers: 1 / hits: 47204  / 10 Years ago, wed, april 2, 2014, 12:00:00

I'm not sure how to escape '+' in regex. Plus can come multiple times in i so we need to replace all + in the string. Here's what I have:



i.replace(new RegExp(+,g),' ').replace(new RegExp(selectbasic=,g),'').split('&');


But this gives me this error:




Uncaught SyntaxError: Invalid regular expression: /+/: Nothing to repeat



More From » regex

 Answers
26

The + character has special significance in regular expressions. It's a quantifier meaning one or more of the previous character, character class, or group.



You need to escape the +, like this:



i.replace(new RegExp(\+,g),' ')...


Or more simply, by using a precompiled expression:



i.replace(/+/g,' ')...

[#71649] Tuesday, April 1, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
benitoh

Total Points: 150
Total Questions: 113
Total Answers: 104

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
benitoh questions
Sun, Mar 21, 21, 00:00, 3 Years ago
Mon, May 13, 19, 00:00, 5 Years ago
;