Nodejs String Replace replaceAll(from, to, caseSensitive)

Here you can find the source of replaceAll(from, to, caseSensitive)

Method Source Code

String.prototype.replaceAll = function(from, to, caseSensitive){
    var target;//from  www  .j a v  a 2 s  .co m
    if (caseSensitive === true) {
        target = new RegExp(from, 'g');
    }else{
        target = new RegExp(from, 'gi');
    }
    return this.replace(target, to);
};

String.prototype.searchAll = function (str, caseSensitive) {
    var target;
    if (caseSensitive === true) {
        target = new RegExp(str, 'g');
    }else{
        target = new RegExp(str, 'gi');
    }
    var results = [];
    while (target.exec(this)) {
        results.push(target.lastIndex - str.length);
    }
    return results;
};

function findElementByText(ele, text){
    var target = $(ele + ":contains(" + text + ")").filter(function(){ return $(this).childElementCount === 0;});
    return target;
}

console.log(findElementByText('a', '?'));
console.log(findElementByText('div', '?'));
console.log(findElementByText('p', '?'));
console.log(findElementByText('b', '?'));
// document.body.innerHTML = document.body.innerHTML.replaceAll('?', '?', false);
// console.log($("p:contains('?')"));

Related

  1. 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;
    };
    
  2. replaceAll(find, replace)
    String.prototype.replaceAll = function(find, replace) {
      var str = this;
      return str.replace(new RegExp(find, 'g'), replace);
    };
    
  3. replaceAll(find, replace)
    String.prototype.replaceAll = function (find, replace) {
        var str = this;
        return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
    };
    
  4. 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);
    };
    
  5. replaceAll(from, to)
    String.prototype.replaceAll = function(from, to)  {
       var str = this;
       while(str.indexOf(from)>=0) {
          str = str.replace(from, to);
       return str;
    
  6. 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;
    };
    
  7. replaceAll(oldString, replacement)
    String.prototype.replaceAll = function (oldString, replacement) {
        var a = this;
        while (a.indexOf(oldString) != -1) {
            a = a.replace(oldString, replacement);
        return a;
    
  8. replaceAll(oldValue, newValue)
    String.prototype.replaceAll = function (oldValue, newValue) {
      return this.replace(new RegExp(oldValue, 'g'), newValue);
    };
    
  9. replaceAll(org, dest)
    String.prototype.replaceAll = function(org, dest) {
      var data = this;
      if (org instanceof Array) {
        org.forEach(function(elm) {
          data = data.split(elm).join(dest);
        });
        return data;
      } else {
        return data.split(org).join(dest);
    ...