Javascript String replaceAll( token, newToken, ignoreCase ) method

Description

Javascript String replaceAll( token, newToken, ignoreCase ) method


String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
    var _token;//  www  . j  a  va2 s  .co  m
    var str = this + "";
    var 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 );
        }

    }
    return str;
};



PreviousNext

Related