title: Complex RegEx replacement
body: <p>I'm trying to write a highlighting functionality. There are two types of highlighting: positive and negative. Positive is done first. Highlighting in itself is very simple - just wrapping keyword/phrase in a <code>span</code> with a specific class, which depends on type of highlighting. </p>
  
  <p><strong><em>Problem:</em></strong><br>
  Sometimes, negative highlighting can contain positive one.  </p>
  
  <p><strong><em>Example:</em></strong><br>
  Original text:  </p>
  
  <blockquote>
    <p>some data from blahblah test was not statistically valid</p>
  </blockquote>
  
  <p>After text passes through positive highlighting "filter", it'll end up like this:  </p>
  
  <pre class="lang-html prettyprint-override"><code>some data from &lt;span class="positive"&gt;blahblah test&lt;/span&gt; was not &lt;span class="positive"&gt;statistically valid&lt;/span&gt;
  </code></pre>
  
  <p>or</p>
  
  <pre class="lang-html prettyprint-override"><code>some data from &lt;span class="positive"&gt;blahblah test&lt;/span&gt; was not &lt;span class="positive"&gt;statistically &lt;span class="positive"&gt;valid&lt;/span&gt;&lt;/span&gt;
  </code></pre>
  
  <p>Then in negative list, we have a phrase <code>not statistically valid</code>.</p>
  
  <p>In both cases, resulting text after passing through both "filters" should look like:  </p>
  
  <pre class="lang-html prettyprint-override"><code>some data from &lt;span class="positive"&gt;blahblah test&lt;/span&gt; was &lt;span class="negative"&gt;not statistically valid&lt;/span&gt;
  </code></pre>
  
  <p><strong><em>Conditions:</em></strong><br>
  - Amount of <code>span</code> tags or their location within keyword/phrase from negative "filter" list is unknown<br>
  - Keyword/phrase must be matched even if it includes <code>span</code> tags (including right before and right after keyword/phrase). These <code>span</code> tags have to be removed.<br>
  - If any <code>span</code> tags are detected, amount of opening and closing <code>span</code> tags removed has to be equal.</p>
  
  <p><strong><em>Questions:</em></strong><br>
  - How to detect these <code>span</code> tags if there are any?<br>
  - Is this even possible with RegEx alone?</p>