replace_all() function in javascript

Usual replace() method of String object in javascript doesn’t do exactly what other similar methods in other languages do. It just don’t replace all occurrences in a string.

In order to replace all matches in the string you need pass regular expression as first parameter in the method, for example like that:

var string = 'blah-blah-blah!';
string = string.replace(/blah/g, 'word');
alert(string);

Read more about regular expression here.

Books to read

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> <pre lang="" line="" escaped="">