Nodejs String Replace replaceAll(search, replace)

Here you can find the source of replaceAll(search, replace)

Method Source Code

// Prototype extensions
String.prototype.replaceAll = function (search, replace) {
    //if replace is not sent, return original string otherwise it will
    //replace search string with 'undefined'.
    if (replace === undefined) {
        return this.toString();
    }//  ww w. ja v a 2  s.c  om

    return this.replace(new RegExp('[' + search + ']', 'g'), replace);
};

Related

  1. replaceAll(s1, s2)
    String.prototype.replaceAll = function(s1, s2) {
        return this.replace(new RegExp(s1, "gm"), s2); 
    };
    String.prototype.replaceAll2Excep = function(s1, s2) {
        var temp = this;
        while (temp.indexOf(s1) != -1) {
            temp = temp.replace(s1, s2);
        return temp;
    ...
    
  2. replaceAll(search, replace)
    String.prototype.replaceAll = function (search, replace) {
        var string        = this,
            replaceLength = replace.length,
            searchLength  = search.length,
            start         = string.indexOf(search);
        while (start !== -1) {
            string = string.substring(0, start) + replace + string.substring(start + search.length);
            start  = string.indexOf(search, start + replaceLength);
        return string;
    };
    
  3. replaceAll(search, replace)
    String.prototype.replaceAll = function(search, replace)
        if(!replace) 
            return this;
        return this.replace(new RegExp('[' + search + ']', 'g'), replace);
    };
    trim11 = function (str) {
        str = str.replace(/^\s+/, '');
        for (var i = str.length - 1; i >= 0; i--) {
    ...
    
  4. replaceAll(search, replace)
    String.prototype.replaceAll = function(search, replace) {
        if (replace === undefined) {
            return this.toString();
        return this.split(search).join(replace);
    
  5. replaceAll(search, replace)
    String.prototype.replaceAll = function (search, replace) {
      return this.replace(new RegExp(search, 'g'), replace);
    };
    String.prototype.ltrim = function () {
      return this.replace(/^\s+/,"");
    };
    String.prototype.rtrim = function () {
      return this.replace(/\s+$/,"");
    };
    ...
    
  6. replaceAll(search, replace)
    String.prototype.replaceAll = function (search, replace) {
        'use strict';
        return this.split(search).join(replace);
    };
    
  7. replaceAll(search, replace)
    String.prototype.replaceAll = function(search, replace){
      return this.split(search).join(replace);
    
  8. replaceAll(search, replace)
    String.prototype.replaceAll = function(search, replace) {
      if(!replace) {
        return this;
      return this.replace(new RegExp(helpers.escapeRegExp(search), 'g'), replace);
    };
    
  9. replaceAll(search, replacement)
    String.prototype.replaceAll = function(search, replacement) {
        var target = this;
        return target.split(search).join(replacement);
    };