String replace all - Node.js String

Node.js examples for String:Replace

Description

String replace all

Demo Code


String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
    var _token, str = this + "", i = -1;
    if ( typeof token === "string" ) {
        if ( ignoreCase ) {
            _token = token.toLowerCase();
            while( ( i = str.toLowerCase().indexOf( token, i >= 0 ? i + newToken.length : 0 ) ) !== -1 ) 
              { str = str.substring( 0, i ) + newToken + str.substring( i + token.length ); }
        } else return this.split( token ).join( newToken );
    }/*from  w  ww. ja va  2  s. c om*/
    return str;
};

Related Tutorials