Chris
Chris

Reputation: 483

Replace all instances of a character from a non-DOM string

I need to take all plus symbols from a variable and replace them with spaces. I have tried:

someVariable = "0+123+45+6";
someVariable.replace(/+/g, ' ');

But this doesn't work... what would be the correct syntax for this situation?

Upvotes: 1

Views: 154

Answers (1)

t.niese
t.niese

Reputation: 40872

the + is a special char (one or more), so you need to escape it.

should be /\+/g

EDIT the regular expression does not modify the string object itself. but returns the result.

someVariable = "0+123+45+6";
someVariable = someVariable.replace(/\+/g, ' ');

Upvotes: 3

Related Questions