title: String.Replace only replaces first occurrence of matched string. How to replace *all* occurrences?
body: <p>Coming from other programming languages, <code>String.replace()</code> typically replaces all occurrences of matching strings. However, that is not the case with <em>javascript/typescript</em>. I found a number of solutions on the web with <em>javascript</em> utilizing regex. I immediately had issues with this solution because of special characters. I suspect there is a way to correct this with regex, but I am not a regex expert. As many have done before me, I created my own method. </p>
  
  <p>Perhaps there are ways to improve performance by making use of a custom <code>StringBuilder()</code> class. I welcome any thoughts.</p>
  
  <pre><code>public static Replace = function (originalString: string, oldValue: string, newValue: string, ignoreCase: boolean = false) {
      //
      // if invalid data, return the original string
      //
      if ((originalString == null) || (oldValue == null) || (newValue == null) || (oldValue.length == 0) )
          return (originalString);
      //
      // do text replacement
      //
      var dest = "";        
      var source: string = originalString;
      if (ignoreCase) 
      {
          source = source.toLocaleLowerCase();
          oldValue = oldValue.toLowerCase();
      }
      //
      // find first match
      //
      var StartPos = 0;
      var EndPos = source.indexOf(oldValue, StartPos);
      var Skip = (EndPos &gt;= 0) ? EndPos - StartPos : source.length-StartPos;   
      //
      // while we found a matched string
      //     
      while (EndPos &gt; -1) {
          //
          // copy original string skipped segment
          //
          if (Skip &gt; 0) dest += originalString.substr(StartPos, Skip);            
          //
          // copy new value
          //
          dest += newValue;
          //
          // skip over old value
          //
          StartPos = EndPos + oldValue.length;
          //
          // find next match
          //
          EndPos = source.indexOf(oldValue, StartPos);
          Skip = (EndPos &gt;= 0) ? EndPos - StartPos : source.length - StartPos;    
      }
      //
      // append the last skipped string segment from original string
      //
      if (Skip &gt; 0) dest += originalString.substr(StartPos, Skip);   
  
      return dest;
  }
  </code></pre>
  
  <p>In order to add support to this method to the <code>string</code> class I added the following code:</p>
  
  <pre><code>interface String { EZReplace(oldValue: string, newValue: string, ignorCase?: boolean): string; }
  
  String.prototype.EZReplace = function (oldValue: string, newValue: string, ignorCase: boolean = false) {
  return EZUtil.Replace(this, oldValue, newValue, ignorCase);}
  </code></pre>
  
  <p>....After re-viewing other posts, I modified the code to use regular expressions. It would be interesting to execute performance tests.</p>
  
  <pre><code> public static Replace = function (originalString: string, oldValue: string, newValue: string, ignoreCase: boolean = false) {
      //
      // if invalid data, return the original string
      //
      if ((originalString == null) || (oldValue == null) || (newValue == null) || (oldValue.length == 0))
          return (originalString);
      //
      // set search/replace flags
      //
      var Flags: string = (ignoreCase) ? "gi" : "g";
      //
      // apply regex escape sequence on pattern (oldValue)
      //
      var pattern = oldValue.replace(/[-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&amp;");
      //
      // replace oldValue with newValue
      //
      var str = originalString.replace(new RegExp(pattern, Flags), newValue);
      return (str);
  }
  </code></pre>