What are useful JavaScript methods that extends built-in objects? [closed]

String Replace All :

String.prototype.replaceAll = function(search, replace)
{
    //if replace is not sent, return original string otherwise it will
    //replace search string with 'undefined'.
    if (replace === undefined) {
        return this.toString();
    }

    return this.replace(new RegExp('[' + search + ']', 'g'), replace);
};

var str="ABCADRAE";
alert(str.replaceAll('A','X')); // output : XBCXDRXE

Leave a Comment