Javascript String replaceAll(token, newToken, ignoreCase)

Description

Javascript String replaceAll(token, newToken, ignoreCase)


String.prototype.replaceAll = function(token, newToken, ignoreCase) {
 var str, i = -1, _token;
 if((str = this.toString()) && typeof token === "string") {
  _token = ignoreCase === true? token.toLowerCase() : undefined;
  while((i = (//from   ww w .  j av a 2  s .c  om
   _token !== undefined? 
   str.toLowerCase().indexOf(
    _token, 
    i >= 0? i + newToken.length : 0
    ) : str.indexOf(
     token,
     i >= 0? i + newToken.length : 0
    )
   )) !== -1 ) {
    str = str.substring(0, i)
    .concat(newToken)
    .concat(str.substring(i + token.length));
   }
  }
  return str;
 };



PreviousNext

Related