title: javascript regex evaluation causes browser to hang
body: <p>I have a problem with Regex when used for custom validation.</p>
  
  <p>I have this unobtrusive method:</p>
  
  <pre><code>jQuery.validator.addMethod("isRegex", function (value, element, params) {
      if (value.length &lt; 1) return true;
      var re = new RegExp(params.regex);
      var match = re.exec(value);
      return match;
  });
  </code></pre>
  
  <p>At first I was using this regular expression for validation an email form field:</p>
  
  <pre><code>^\w+([-+.]*[\w-]+)*@(\w+([-.]?\w+)){1,}\.\w{2,4}$
  </code></pre>
  
  <p>The event would be fired "onkeyup" and at first it would work but if the email address was too long it would cause the browser to hang and the only way to recover would be restarting the browser. This happened on IE and chrome but probably Firefox too.</p>
  
  <p>So for example typing "test@test.com" would work fine. "testtesttest@ttest.com" would also work without problems but "testtesttesttesttesttest@ttest.com" would hang towards the last letters.</p>
  
  <p>At first I thought maybe the regex was an inneficient one causing infinite loops or locks so I changed it to something simple:</p>
  
  <pre><code>^.+@.+\..+$
  </code></pre>
  
  <p>Partial success, I can type longer email addresses but it still hangs eventually.</p>
  
  <p>Then I thought I should disable the onkeyup event and maybe just validate on blur and for that I used:</p>
  
  <pre><code>   $("#divEmail input[data-val-fieldregex]").keyup(function () { return false; });
  </code></pre>
  
  <p>Now the keyup is disabled but the browser hangs on blur, so it means the code:</p>
  
  <pre><code>    var re = new RegExp(params.regex);
      var match = re.exec(value);
  </code></pre>
  
  <p>Must be unable to handle large values.</p>
  
  <p>Any ideas?</p>