title: Insert characters on start and end of regex result on javascript
body: <p>i have problems inserting character in result of regex. I need do some like this:</p>
  
  <pre><code>var str = "Hello world, hello";
  var regX = /he/ig;
  </code></pre>
  
  <p>The result have to be a string like this:</p>
  
  <pre><code>console.log(result); 
  &lt;mark&gt;He&lt;/mark&gt;llo world, &lt;mark&gt;he&lt;/mark&gt;llo"
  </code></pre>
  
  <p>I tried using this code: </p>
  
  <pre><code>r = /he/ig;
  str = "Hello world Hello";
  var match, indexes = [];
  while (match= r.exec(str)){
    indexes.push([match.index, match.index+match[0].length]);
  }
  
  indexes.forEach( (element) =&gt; {
     var strStart = str.substring(0,element[0]);
     var strBetween = "&lt;mark&gt;"+str.substring(element[0],element[1])+"&lt;/mark&gt;";
     var strEnd = str.substring(element[1],str.length);
     str = strStart.concat(strBetween,strEnd);
   });
  
   console.log(str); //&lt;mark&gt;He&lt;/mark&gt;llo worl&lt;mark&gt;d &lt;/mark&gt;Hello                   
  </code></pre>
  
  <p>I understand where is the error, but i don't kwon how solve that.</p>