"-x-x+x".replace('-', '+')
equals
"+x-x+x"
why isn't the second -
replaced?
"-x-x+x".replace('-', '+')
equals
"+x-x+x"
why isn't the second -
replaced?
.replace
only replaces the first instance it finds. To replace all of them, use a regex:
"-x-x+x".replace(/-/g, '+')
Note the /g
at the end of the regex: it indicates "global" mode. Without it you'll still only replace the first instance.
This is explained in the documentation for String#replace
:
Use a regular expression:
'-x-x+x'.replace(/-/g, '+')
//=> "+x+x+x"