开发者

As of Firefox 4, regular expression throws ""Regular expression too complex" error

开发者 https://www.devze.com 2023-03-26 05:53 出处:网络
I have the following email address validator, which won\'t evaluate and throws the above error when the string to evaluate hits a certain length:

I have the following email address validator, which won't evaluate and throws the above error when the string to evaluate hits a certain length:

^([\w\-]+)([\.\w\-\+]+)*@([\w\-]+\.){1,5}([A-Za-z]){2,4}$

It's easy to reproduce. Drop the regex into http://regexpal.com/ along with a long ema开发者_JS百科il address such as juicy.cakeballs2@cheeze-party.poonalicious.com

The problem is specific to Firefox 4+

Can anyone suggest perhaps a way to simplify the regex or some other way to address the problem?

It's tearing me apart, Lisa!


The problem is with the overlapping classes. [ab]+[abc]+ can match "ababababc" a number of different ways. To reduce backtracking, refactor it to something like [ab]+(c[ab]*)* instead. (I know, the refactored expression doesn't match exactly the same strings as the original expression. Too lazy to post a proper example. Google "regex backtracking", no, buy and read Friedl, now).

For the record, forms which want to "validate" my email address in JavaScript fail most of the time. I can't count how many times I had to create a temporary email address just in order to be able to complete a purchase or subscription. The only reliable way to validate an email address is to attempt to send email. Please don't create yet one more broken form "validator".

Some example addresses which are technically valid:

*@example.com
me@had.to.create.a.temp.address.again.i.run.my.own.dns.example.net
you.forgot@domain.example.museum
0

精彩评论

暂无评论...
验证码 换一张
取 消