title: Using javascript regexp to find the first AND longest match
body: <p>I have a RegExp like the following simplified example:</p>
  
  <pre><code>var exp = /he|hell/;
  </code></pre>
  
  <p>When I run it on a string it will give me the first match, fx:</p>
  
  <pre><code>var str = "hello world";
  var match = exp.exec(str);
  // match contains ["he"];
  </code></pre>
  
  <p><strong>I want the first and longest possible match</strong>,
  and by that i mean sorted by index, then length.</p>
  
  <p>Since the expression is combined from an array of RegExp's, I am looking for a way to find the longest match without having to rewrite the regular expression.</p>
  
  <p>Is that even possible?</p>
  
  <p>If it isn't, I am looking for a way to easily analyze the expression, and arrange it in the proper order. But I can't figure out how since the expressions could be a lot more complex, fx:</p>
  
  <pre><code>var exp = /h..|hel*/
  </code></pre>