This seems so simple and trivial but it is not working. Here is my javascript:
var url = "/computers/";
console.log(url);
url.replace(/\//gi, " ");
console.log(url);
And here is the output in my browsers console:
/computers/
/computers/
As you can see nothing changes. As you can tell from the code I'm trying to replace the forward slashes with spaces. What am I doing wrong?
Jonathan Leffler
760k145 gold badges962 silver badges1.3k bronze badges
asked Jun 2, 2012 at 14:48
greatwitenorth
2,4512 gold badges21 silver badges22 bronze badges
3 Answers 3
url = url.replace(/\//gi, " ");
answered Jun 2, 2012 at 14:49
galchen
5,3003 gold badges32 silver badges44 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
greatwitenorth
man I feel a bit dumb. Thanks for that.
netpoetica
LoL. Epic. I just ran into this same issue and this answer saved me from emailing Brendan Eich to tell him that JS replace doesn't work :)
Cԃաԃ
@greatwitenorth ᄏᄏᄏᄏ did the same mistake.
morkro
Gosh, was fighting for an hour with that. Feeling dumb as hell now. Thanks for saving me!
Nothing changes because you're not assigning the result of the replacement to a variable. Add url = url.replace()
answered Jun 2, 2012 at 14:51
dda
6,2212 gold badges27 silver badges37 bronze badges
Comments
url.replace(/\//gi, " "); returns the resulting string (in javascript you can't modify an existing string), you are not assigning it to anything
assign it like so:
url = url.replace(/\//gi, " ");
answered Jun 2, 2012 at 14:51
Esailija
140k24 gold badges280 silver badges328 bronze badges
Comments
lang-js