Wednesday, October 6, 2010

javascript replace all character occurances in one line

So if I have a string:

var myOldString = "x%=
%%%%0.4218%%%%0.7922
%%%%0.9157%%%%0.9595
";


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: