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");
Dan Davies Brackett
10.1k2 gold badges36 silver badges55 bronze badges
asked Mar 27, 2012 at 21:40
Christian
1,6582 gold badges16 silver badges26 bronze badges
4 Answers 4
You need to escape the plus sign with a backslash, like so:
var temps ="some+thing+-+More";
temps = temps.replace(/\+/g, "blank");
answered Mar 27, 2012 at 21:42
Jonathan Fingland
57.3k11 gold badges89 silver badges79 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
"+ + + +".replace(/\+/g, "blank")
This results:
"blank blank blank blank"
You should use the escape char:
temps = temps.replace(/\+/g, "blank");
answered Mar 27, 2012 at 21:43
oruchreis
8662 gold badges12 silver badges28 bronze badges
Comments
temps = temps.replace(/\+/g, "blank");
Balaswamy Vaddeman
8,6003 gold badges33 silver badges40 bronze badges
answered Mar 27, 2012 at 21:44
j08691
209k33 gold badges269 silver badges281 bronze badges
Comments
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!
1 Comment
Jonathan Fingland
This is actually a really good technique for lots of other situations. \x for the hex value and \u for unicode. For the OP's case it is a bit overkill but most developers should be aware of this none the less.
lang-js
/[+]/gor you can just escape it:/\+/g."/+"actually has a different meaning - namely just these two characters. The escape character in JavaScript is\.