I got a bugged regex "\w+([\.\-]?\w+)*@"
when it try to test whether it matchs a string
当它试图测试它是否匹配字符串时,我得到了一个带错误的正则表达式“\ w +([\。\ - ]?\ w +)* @”
"ffffffffffb3ffffffffffafffffffffffabffffffffffc2ffffffffffa7e"
it will cause IE and Chrome hung up. But works fine by FF.
它会导致IE和Chrome挂起。但FF工作得很好。
I found out that the "?" in the regex is not necessary. And it works find after I remove the "?".
我发现了“?”在正则表达式是没有必要的。它删除了“?”后找到它。
But here is what I don't understand what cause the problem. Here is some test
但这是我不明白导致问题的原因。这是一些测试
-
"\w+([\.\-]?\w+)*"
works fine.“\ w +([\。\ - ]?\ w +)*”工作正常。
-
"\w+([\.\-]\w+)*@"
works fine.“\ w +([\。\ - ] \ w +)* @”工作正常。
-
"\w+([\.\-]?\w+)*@"
cause the problem“\ w +([\。\ - ]?\ w +)* @”导致问题
Anyone knows why? or it's just the performance between browsers.
谁知道为什么?或者只是浏览器之间的性能。
1 个解决方案
#1
5
This is called catastrophic backtracking.
这被称为灾难性的回溯。
In your third example, the @
(which obviously causes the regex to fail) forces your regex engine to try all possible permutations of \w+(\w+)*
(since the character class is optional). With a string of this length, the calculations would take longer than until the heat death of the universe.
在你的第三个例子中,@(显然导致正则表达式失败)强制你的正则表达式引擎尝试所有可能的\ w +(\ w +)*的排列(因为字符类是可选的)。使用这个长度的字符串,计算将花费更长的时间,直到宇宙的热量死亡。
Firefox appears to have an iteration limit on regexes and will abort after about a million attempts, Chrome and IE seem to be a bit more stoic here.
Firefox似乎对正则表达式有一个迭代限制,并且在大约一百万次尝试后将中止,Chrome和IE似乎在这里更加坚忍。
#1
5
This is called catastrophic backtracking.
这被称为灾难性的回溯。
In your third example, the @
(which obviously causes the regex to fail) forces your regex engine to try all possible permutations of \w+(\w+)*
(since the character class is optional). With a string of this length, the calculations would take longer than until the heat death of the universe.
在你的第三个例子中,@(显然导致正则表达式失败)强制你的正则表达式引擎尝试所有可能的\ w +(\ w +)*的排列(因为字符类是可选的)。使用这个长度的字符串,计算将花费更长的时间,直到宇宙的热量死亡。
Firefox appears to have an iteration limit on regexes and will abort after about a million attempts, Chrome and IE seem to be a bit more stoic here.
Firefox似乎对正则表达式有一个迭代限制,并且在大约一百万次尝试后将中止,Chrome和IE似乎在这里更加坚忍。