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');

Read more about regular expression here.

Books to read

Leave a Reply

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