Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
419 views
Welcome To Ask or Share your Answers For Others

1 Answer

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,' ')...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...