Christian
Christian

Reputation: 1628

changing a variable in a string javascript

I have a string looking like: some+thing+-+More

How do i replace the + sign?

I have tried the following without success:

temps = "some+thing+-+More";
temps = temps.replace("/+" /g, "blank");
temps = temps.replace("+" /g, "blank");
temps = temps.replace(/+/g, "blank");

Upvotes: 0

Views: 102

Answers (4)

Christian
Christian

Reputation: 1628

Thanks Jonathan.

I took a different approach: I figure out the Hex number for + sign was 2B So....

temps = temps = temps.replace(/\x2B/g, "blank"); 

also did the trick!

Upvotes: 0

j08691
j08691

Reputation: 207901

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

jsfiddle example

Upvotes: 0

oruchreis
oruchreis

Reputation: 866

"+ + + +".replace(/\+/g, "blank")

This results:

"blank blank blank blank"

You should use the escape char:

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

Upvotes: 0

Jonathan Fingland
Jonathan Fingland

Reputation: 57167

You need to escape the plus sign with a backslash, like so:

var temps ="some+thing+-+More";
temps = temps.replace(/\+/g, "blank");

Upvotes: 8

Related Questions