Skip to content
Advertisement

How to use “for” along with string replace?

I want to make several replacements in the same string. I have this:

JavaScript

after the replacements it should look like this:

JavaScript

So I tried:

JavaScript

but it doesnt seem to do anything. What is wrong?

Advertisement

Answer

The way you wrote it now, it will only replace on the last id and assign it to the mylist variable, because .replace does not mutate the string.

If we assign to userlist instead of mylist it will work the way you want it to work, because then we’re actually modifying the string on every iteration:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement