JavaScript String Replace All

The JavaScript function for String Replace replaces the first occurrence in the string. The function is similar to the php function str_replace and 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…

[js]str = str.replace(”find”,”replace”)[/js]

To ReplaceAll you have to do it a little differently. To replace all occurrences in the string, use the g modifier like this:

[js]str = str.replace(/find/g,”replace”)[/js]

5 thoughts on “JavaScript String Replace All”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>