Pages

Thursday, April 21, 2016

Replace all in Javascript

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

};

No comments:

Post a Comment