Friday, April 29, 2011

Javascript String Replace All Function

JavaScript String Replace function only replaces the first occurrence in the string. Replace function takes two simple parameters, The first parameter is the pattern to find and the second parameter is the string to replace the pattern with when found. The javascript function does not Replace All...

str = str.replace(”Pattern”,”ReplaceString”)

To ReplaceAll you have to do it a little differently. To replace all occurrences in the string, use the g modifier like this:
str = str.replace(/Pattern/g,”ReplaceString”)
Submit this story to DotNetKicks