5

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?

0

3 Answers 3

21
url = url.replace(/\//gi, " ");
Sign up to request clarification or add additional context in comments.

4 Comments

man I feel a bit dumb. Thanks for that.
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 :)
@greatwitenorth ㅋㅋㅋㅋ did the same mistake.
Gosh, was fighting for an hour with that. Feeling dumb as hell now. Thanks for saving me!
2

Nothing changes because you're not assigning the result of the replacement to a variable. Add url = url.replace()

Comments

0

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, " ");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.