regex - Firefox length limit in regular expression (is it a bug?) -
i'm debugging problem in application uses regular expression validate emails clientside (yeah know, both kinda stupid) , problem got me stumped.
the thing validation works fine in chrome fails in firefox , wonder if bug or if there's wrong regular expression causes error.
please check fiddle complete test case: http://jsfiddle.net/kqvgj/
new regexp(/^\s+([\_\-\.]*\s+[\_\-]?)*@\s+([\_\-]?\s+)*\.+([\-\_]?\s)+(\.?\s+)*$/);
in firefox regex above matches mw@thisissometest.de
not mw@thisissometestbutlong.de
.
it seems fail based on length of input alone, there no length restriction in expression @ all!?
the fault in regex: it's pathologically inefficient. basically, you've got multiple consecutive parts can match same characters, controlled open-ended quantifiers (*
, +
). creates astronomical number of "paths" regex has check before gives on match. in fact, sort of problem becomes apparent when no match possible, you've managed trigger on regex should match.
i suspect trying this:
/^[a-z]+(?:[_.-][a-z]+)*@[a-z]+(?:\.[a-z]+)*$/i
before starts criticizing, know [a-z]+
no more correct \s+
. i'm trying explain what's wrong regex. idea force user name , domain name start letters, while allowing them separated chunks around delimiters .
, -
, , _
. that's makes complicated
the important feature of regex moves forward. when [a-z]+
runs out of letters consume, next thing sees must 1 of delimiter characters, at-sign ('@'), or end of string (depending on part of address it's matching). if doesn't see expects, match attempt fails immediately.
in regex \s+
part gobbles whole string, starts backing off 1 character @ time give next part chance match. process repeated every \s+
. hamza observed, that's regex engine spends of time. it's not \s+
alone that's killing you, it's structure of regex.
Comments
Post a Comment