title: Javascript regex: Find all URLs optimization
body: <p>This question is a follow-up for the following post:
  <a href="https://stackoverflow.com/questions/35553751/javascript-regex-find-all-urls-outside-a-tags-nested-tags">Javascript regex: Find all URLs outside &lt;a&gt; tags - Nested Tags</a></p>
  
  <p>I discovered that the code:</p>
  
  <pre><code>\b((https?|ftps?):\/\/[^"&lt;\s]+)(?![^&lt;&gt;]*&gt;|[^"]*?&lt;\/a)
  </code></pre>
  
  <p>is extremely inefficient compared to executing it separately for <code>http</code> and <code>ftp</code> part like this:</p>
  
  <pre><code>\b(https?:\/\/[^"&lt;\s]+)(?![^&lt;&gt;]*&gt;|[^"]*?&lt;\/a)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>\b(ftps?:\/\/[^"&lt;\s]+)(?![^&lt;&gt;]*&gt;|[^"]*?&lt;\/a)
  </code></pre>
  
  <p>Here are examples at regex101.com:</p>
  
  <ul>
  <li>1st method - <a href="https://regex101.com/r/aM0oM3/1" rel="nofollow noreferrer">6395 steps</a></li>
  <li>2nd method - <a href="https://regex101.com/r/aM0oM3/2" rel="nofollow noreferrer">3393 steps</a> + <a href="https://regex101.com/r/aM0oM3/3" rel="nofollow noreferrer">863 steps</a></li>
  </ul>
  
  <p>However, in one of my HTML page these codes compares as <strong>85628</strong> steps vs. <strong>7258 + 795</strong> steps, that is quite insane.</p>
  
  <p>As far as I have seen, using <a href="http://www.w3schools.com/jsref/jsref_regexp_xy.asp" rel="nofollow noreferrer">(x|y)</a> pattern reduces the execution length but here probably for a strange reason it is otherwise.</p>
  
  <p>Any help would be appreciated.</p>