title: Replacing keyword in string that is HTML page using Java
body: <p>I have following string:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;meta&gt;...&lt;/meta&gt;&lt;head&gt;
  &lt;body&gt;
     &lt;div id="foo"&gt;
       Text I want to search &amp; replace occurrences
       of keywords such as Foo or foo while ignoring case
     &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What I want to end up with is: </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;meta&gt;...&lt;/meta&gt;&lt;head&gt;
  &lt;body&gt;
     &lt;div id="foo"&gt;
       Text I want to search &amp; replace occurrences
       of keywords such as &lt;b&gt;Foo&lt;/b&gt; or &lt;b&gt;foo&lt;/b&gt; while ignoring case
     &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So pretty much I'd like to search for and replace <code>foo</code> with <code>&lt;b&gt;foo&lt;/b&gt;</code> or <code>&lt;b&gt;Foo&lt;/b&gt;</code>. It is important to retain the case of the string that is being replace but match it with keyword <code>foo</code> while ignoring the case of the matches. </p>
  
  <p>Another important thing is that the replacement ignores all html tags and their content. Notice that <code>&lt;div id="foo"&gt;</code> stays like it is. </p>
  
  <p>I drafted this but didn't test it yet</p>
  
  <pre><code> text = text.replace("(?i)"+keyword+"(?!([^&lt;]+)?&gt;)", "&lt;b&gt;"+keyword+"&lt;/b&gt;");
  </code></pre>
  
  <p>The problem with the above is that it doesn't remember the case of word that is being replaced and just puts in the keyword.</p>