Nodejs String Replace replaceAll(org, dest)

Here you can find the source of replaceAll(org, dest)

Method Source Code

String.prototype.replaceAll = function(org, dest) {
   var data = this;
   if (org instanceof Array) {
      org.forEach(function(elm) {
         data = data.split(elm).join(dest);
      });//from w  w w.  ja v  a2 s . co m
      return data;
   } else {
      return data.split(org).join(dest);
   }
};

var common = {};

common.randomAlphaNumeric = function(digit) {
   var text = '';
   var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
   for (var i = 0; i < digit; i++) {
      text += possible.charAt(Math.floor(Math.random() * possible.length));
   }
   return text;
};

Related

  1. replaceAll(from, to)
    String.prototype.replaceAll = function(from, to)  {
       var str = this;
       while(str.indexOf(from)>=0) {
          str = str.replace(from, to);
       return str;
    
  2. 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);
    };
    ...
    
  3. 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;
    };
    
  4. replaceAll(oldString, replacement)
    String.prototype.replaceAll = function (oldString, replacement) {
        var a = this;
        while (a.indexOf(oldString) != -1) {
            a = a.replace(oldString, replacement);
        return a;
    
  5. replaceAll(oldValue, newValue)
    String.prototype.replaceAll = function (oldValue, newValue) {
      return this.replace(new RegExp(oldValue, 'g'), newValue);
    };
    
  6. replaceAll(org, dest)
    String.prototype.replaceAll = function(org, dest) {
        return this.split(org).join(dest);
    
  7. replaceAll(org, tag)
    String.prototype.replaceAll = function(org, tag){
      return this.replace(new RegExp(org, 'g'), tag);
    };
    
  8. replaceAll(origin, replace)
    String.prototype.replaceAll = function(origin, replace){
      var regexp = new RegExp(origin,'g');
      return this.replace(regexp,replace);
    };
    
  9. replaceAll(reallyDo, replaceWith, ignoreCase)
    String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {
        if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
            return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);
        } else {
            return this.replace(reallyDo, replaceWith);
    };