title: Why does this JavaScript regex crash in the browser?
body: <p>To my knowledge <code>[ab]</code> and <code>(a|b)</code> should be equivalent in purpose when trying to match against a set of characters. Now, look at two regex:</p>
  
  <pre><code>/^(\s|\u00A0)+|(\s|\u00A0)+$/g
  /^[\s\u00A0]+|[\s\u00A0]+$/g
  </code></pre>
  
  <p>They should both match against whitespaces at the beginning and end of a string (See section on Polyfill <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/trim" rel="nofollow">here</a> for more info on the regex itself). When using square brackets all things work well, but when you switch to parenthesis even the simplest of strings causes the browser to run seemingly indefinitely. This happens on latest Chrome and Firefox.</p>
  
  <p><a href="http://jsfiddle.net/parhamfh/ff0sk35k/" rel="nofollow">This jsfiddle</a> demonstrates this:</p>
  
  <pre><code>a ="a                                                               b";
  
  // Doesn't work
  // alert(a.replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,''));
  // Works
  alert(a.replace(/^[\s\u00A0]+|[\s\u00A0]+$/g,''));
  </code></pre>
  
  <p>Is this a crazy quirk with the browser's implementation of the regex engine or is there something else about the regex's algorithm which causes this? </p>