Skip to content Skip to sidebar Skip to footer

Replacing All Plus Symbols In Javascript String

I want to replace all + symbols in a JavaScript String with a space. Based on this thread Fastest method to replace all instances of a character in a string and this thread How to

Solution 1:

You need to escape the + since it is a special character in regex meaning "one or more of the previous character." There is no previous character in /+/, so the regex does not compile.

soql = soql.replace(/\+/g, " ");
//or
soql = soql.replace(/[+]/g, " ");

Solution 2:

Try escaping the +:

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

The + in a regular expression actually means "one or more of the previous expression (or group)". Escaping it tells that you want a literal plus sign ("+").

More information on quantifiers is available if you google it. You might also want to look into the Mastering Regular Expressions book.

Post a Comment for "Replacing All Plus Symbols In Javascript String"