Nodejs Utililty Methods String Replace

List of utility methods to do String Replace

Description

The list of methods to do String Replace are organized into topic(s).

Method

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);
replaceAll(find, replace)
String.prototype.replaceAll = function (find, replace) {
  return this.replace(new RegExp(find, 'g'), replace);
};
replaceAll(find, replace)
String.prototype.replaceAll = function(find, replace){
  find = find.escapeRegExp();
  return this.replaceAllRegExp(find, replace);
};
replaceAll(find, replace)
String.prototype.replaceAll = function (find, replace) {
  return this.replace(new RegExp(find, 'g'), replace);
};
String.prototype.sanitize = function () {
  var s = this.replace(/\W+/g, "");
  return s;
};
replaceAll(find, replace)
String.prototype.replaceAll = function(find, replace) {
  var str = this;
  return str.replace(new RegExp(find, 'g'), replace);
};
replaceAll(find, replace)
String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};
replaceAll(from, to)
String.prototype.replaceAll = function (from, to) {
  if (from == ".")
    return this.replace(/\./g, to);
  var rgx = new RegExp(from, 'g');
  return this.replace(rgx, to);
};
replaceAll(from, to)
String.prototype.replaceAll = function(from, to)  {
   var str = this;
   while(str.indexOf(from)>=0) {
      str = str.replace(from, to);
   return str;
replaceAll(from, to, caseSensitive)
String.prototype.replaceAll = function(from, to, caseSensitive){
    var target;
    if (caseSensitive === true) {
        target = new RegExp(from, 'g');
    }else{
        target = new RegExp(from, 'gi');
    return this.replace(target, to);
};
...
replaceAll(oldStr, newStr)
String.prototype.replaceAll = function(oldStr, newStr){
  let s = this.toString();
  if(oldStr instanceof RegExp){ let a = this.match(oldStr); oldStr = a === null ? '' : a[0]; }
  if(oldStr !== '') while(s.indexOf(oldStr) != -1){ s = s.replace(oldStr, newStr) }
  return s;
};