Wednesday, October 6, 2010

javascript replace all character occurances in one line

So if I have a string:

var myOldString = "x%=

And I want to replace all of the % with space I can use javascript and use the '/g' modifier to replace all:

temp = myOldString.replace(/%/g,' ');

If I had done:

temp = myOldString.replace('%',' ');

It would have only replaced the first occurance which was not what I wanted.

No comments: