How can I replace a plus sign in JavaScript?

2019-02-04 02:28发布

问题:

I need to make a replace of a plus sign in a javascript string. there might be multiple occurrence of the plus sign so I did this up until now:

myString= myString.replace(/+/g, "");#

This is however breaking up my javascript and causing glitches. How do you escape a '+' sign in a regular expression?

回答1:

myString = myString.replace(/\+/g, "");


回答2:

You need to escape the + as its a meta char as follows:

myString= myString.replace(/\+/g, "");

Once escaped, + will be treated literally and not as a meta char.



回答3:

I prefer this:

myString.replace(/[+]/g, '').


回答4:

you should escape your + sign, \+



回答5:

myString.replace(/\+/g, "");