title: Using regexes to modify the text of html (with javascript)
body: <p>I want to modify the text in a html file using javascript in an android webview.</p>
  
  <p>Essentially, I want to do what android Linkify does to text, but I don't want to do it with java code, because I feel like that might delay the webview rendering the html (if I parse the text before sending it to the webview).</p>
  
  <p>So, for example a piece of html like this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;a href="http://www.google.com"&gt;google.com&lt;/a&gt; &lt;!--these two shouldn't be linked--&gt;
      &lt;a href="mailto:akhilcherian@gmail.com"&gt;akhilcherian@gmail.com&lt;/a&gt; &lt;!--these two shouldn't be linked--&gt;
      &lt;p&gt;www.google.com&lt;/p&gt; &lt;!--this should be linked--&gt;
      &lt;p&gt;102-232-2312 2032-122-332 &lt;/p&gt;&lt;!-- should be linked as numbers--&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Should become this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
      &lt;a href="http://www.google.com"&gt;google.com&lt;/a&gt;
      &lt;a href="mailto:akhilcherian@gmail.com"&gt;akhilcherian@gmail.com&lt;/a&gt;
      &lt;p&gt;&lt;a href="http://www.google.com"&gt;www.google.com&lt;/a&gt;&lt;/p&gt;
      &lt;p&gt;&lt;a href="tel:102-232-2312"&gt;102-232-2312&lt;/a&gt; &lt;a href="tel:2032-122-332&gt;2032-122-332&lt;/a&gt; &lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I <strong>already have the regexes</strong> to convert numbers and email ids to links, and they're working well enough. <strong>What I want to ensure is that I don't link anything that's already within tags</strong>. I've removed anchor tags, so they're not an issue, but I also need to avoid linking things like this:</p>
  
  <pre><code>&lt;div width="1000"&gt; &lt;!-- Don't want this '1000' to be linked (but I do want other 4 digit numbers to be)--&gt;
  </code></pre>
  
  <p>So for example if my regex for links is:</p>
  
  <pre><code>var replacePattern1 = /((https?|ftp|file):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;]*[-A-Z0-9+&amp;@#\/%=~_|])/gim
  </code></pre>
  
  <p>How do I make sure that it's not within &lt; and >? (Answers using javascript would be appreciated, but if you feel like this is a stupid way of doing it, please let me know about alternatives).</p>
  
  <p>If you're answering with javascript, this question can essentially be shortened to:</p>
  
  <p>How do I write a regex in javascript to search for patterns which are not surrounded by '&lt;' '>' tags</p>