title: replace substring between two substrings on every match
body: <p>Is there a faster built-in function in JavaScript to replace a substring2 between two substrings with a sequence of numbers the length of substring2, than looping the whole string and replace it by hand.</p>
  
  <p>example:</p>
  
  <p>substring before: <code>"before"</code></p>
  
  <p>substring after: <code>"after"</code></p>
  
  <p>if <code>substring2 is length of 3</code> => replace with <code>string 011</code></p>
  
  <p>if <code>substring2 is length of 6</code> => replace with <code>string 999999</code>
  string: </p>
  
  <p><code>"beforeoooafter beforeaftebefore123456afteradf"</code></p>
  
  <p>ooo would be the substring2 and 123456 too</p>
  
  <p>ooo => 011 (because length 3)</p>
  
  <p>123456 => 999999 (because length 6)</p>
  
  <p>substring2 is a match between the string before and after</p>
  
  <p>result: </p>
  
  <p><code>"before011after beforeaftebefore999999afteradf"</code></p>