title: Splitting a string and wrapping math tags in that string
body: <p>I have a string that contains math. I need to wrap the math tags in a span and put the string back together. I have accomplished this for a specific case but I am looking for something far simpler(if there is) and a more efficient way to accomplish this. For instance if there is text before the math Tag this would not work or if there are multiple math tags this would not work. Thanks for any help you can give.</p>
  
  <p><strong>String</strong></p>
  
  <pre><code>&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;&amp;#xA0;&lt;/mo&gt;&lt;msqrt&gt;&lt;mn&gt;34&lt;/mn&gt;&lt;/msqrt&gt;&lt;/math&gt; I am text after the string.
  </code></pre>
  
  <p><strong>Desired String</strong></p>
  
  <pre><code>&lt;span class="math math-inline"&gt;&lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;&amp;#xA0;&lt;/mo&gt;&lt;msqrt&gt;&lt;mn&gt;34&lt;/mn&gt;&lt;/msqrt&gt;&lt;/math&gt;&lt;/span&gt; I am text after the string.
  </code></pre>
  
  <p><strong>Solution So far</strong></p>
  
  <pre><code>let spanStart = '&lt;span class="math math-inline"&gt;';
  let spanEnd = '&lt;/span&gt;';
  let mathStart = '&lt;math';
  let mathEnd = '&lt;/math&gt;';
  let startPos = mathString.indexOf(mathStart);
  let endPos = mathString.indexOf(mathEnd);
  
  let output = mathString.substr(startPos, 0) + spanStart + mathStart + mathString.substr(mathStart.length, (endPos - mathStart.length)) + mathString.substr(endPos, mathEnd.length) + spanEnd + mathString.substr((endPos + spanEnd.length));
  </code></pre>
  
  <p>I have made a <a href="https://codepen.io/bilcker/pen/gEeXdW" rel="nofollow noreferrer">pen to show my solution</a> so far but again it will not work all the time.</p>
  
  <p>For instance if had:</p>
  
  <pre><code>This is question 2 &lt;math xmlns="http://www.w3.org/1998/Math/MathML"&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;&amp;#xA0;&lt;/mo&gt;&lt;msqrt&gt;&lt;mn&gt;34&lt;/mn&gt;&lt;/msqrt&gt;&lt;/math&gt; I am text after the string.
  </code></pre>
  
  <p>It would not longer be correct. Thanks for your help</p>