Javascript String replaceAll(find, replace)

Description

Javascript String replaceAll(find, replace)


String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find, 'gi'), replace);
};

Javascript String replaceAll(find, replace)

String.prototype.replaceAll = function (find, replace) {
  var str = this;
  return str.replace(new RegExp(find, 'g'), replace);
};

Javascript String replaceAll(find, replace)

// taken from http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript on 20150330 at 11:12 EST
// extended to be a prototype
String.prototype.replaceAll = function (find, replace) {
 return this.replace(new RegExp(find, 'g'), replace);
};

Javascript String replaceAll(find, replace)

function escapeRegExp(string) {
    return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

String.prototype.replaceAll = function (find, replace) {
    return this.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

Javascript String replaceAll(find, replace)

String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};

Javascript String replaceAll(find, replace)

String.prototype.replaceAll = function(find, replace){
 find = find.escapeRegExp();
 return this.replaceAllRegExp(find, replace);
};

Javascript String replaceAll(find, replace)

// replaceAll//from  w  ww. j  a  va 2  s . c  o  m
String.prototype.replaceAll = function(find, replace) {
 var str = this;
 return str.replace(new RegExp(find, 'g'), replace);
};

// startsWith
String.prototype.startsWith = function(str){
 return this.indexOf(str) === 0;
};

Javascript String replaceAll(find, replace)

// This function is used to create return id's that do not contains
// characters that conflict with event handlers
String.prototype.replaceAll = function (find, replace) {
  return this.replace(new RegExp(find, 'g'), replace);
};

String.prototype.sanitize = function () {

  /*var s = this.replaceAll(" ", "_");
  s = s.replaceAll(",", "");//from   w  ww  .j av a 2s .  c  o  m
  s = s.replace("&", "");
  s = s.replace(":", "");
  s = s.replace(".", "");
  s = s.replace(/\(|\)/g,'');
  console.log(s);*/

  var s = this.replace(/\W+/g, "");
  return s;
};



PreviousNext

Related