Replace all in Javascript

5:43 PM

I was notice that when you want to replace a string-1 to the another string-2 and the string-1 present on more the one place the the js replace function is not work properly, so here I've write a js prototype function for this :

String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);

};

You Might Also Like

0 comments