Monday, August 16, 2010

JavaScript String Replace All... and Then Some...

Need to replace text in a JavaScript string? Use string.replace and you're done. Unless, of course, there are multiple instances to be replaced, since the string.replace() function only replaces the first occurrence.

I had that problem, and found Brandino's article, JavaScript String Replace All to be just the trick.

It seems that you can use a regular expression as the target of the string.replace function.

In fact, in addition to being able to do multiple replaces on a single target, this also would allow you to create a regex target for your replace that would replace different things with the same replacement text... which could be handy if you need to remove or replace certain unwanted characters throughout a string...