Javascript String replaceAll(pattern, replacement)

Description

Javascript String replaceAll(pattern, replacement)


String.prototype.replaceAll = function(pattern, replacement) {
 var tokens = this.split(pattern);
 
 if (tokens.length > 0) {
  var newString = tokens[0];
  
  for (var i = 1, n = tokens.length; i < n; i++) {
   newString = newString.concat(replacement, tokens[i]);
  }//from  w  w w. j  a  v a 2s . c  o  m
  
  return newString;
 }
 
 return this;
};



PreviousNext

Related