当使用IE& amp;匹配正则表达式时,javascript会挂起铬 [英] javascript hang when matching regular expression using IE & Chrome

查看:146
本文介绍了当使用IE& amp;匹配正则表达式时,javascript会挂起铬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试了一个带有错误的正则表达式\ w +([\。\ - ]?\ w +)* @
测试它是否匹配字符串

I got a bugged regex "\w+([\.\-]?\w+)*@" when it try to test whether it matchs a string

"ffffffffffb3ffffffffffafffffffffffabffffffffffc2ffffffffffa7e"

这将导致IE和Chrome挂起。但FF工作正常。

it will cause IE and Chrome hung up. But works fine by 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


  1. \ w +([\。\ - ]?\ w +)*工作正常。

\ w +([\\ \\-] \ w +)* @工作正常。

\ w +([\.\ - ]?\ w +)* @导致问题

任何人都知道为什么?或者只是浏览器之间的性能。

Anyone knows why? or it's just the performance between browsers.

推荐答案

这叫做灾难性的回溯

在你的第三个例子中, @ (这显然导致正则表达式失败)强制你的正则表达式引擎尝试所有可能的 \w +(\w +)* 的排列(因为字符类是可选的)。使用这个长度的字符串,计算将花费更长的时间,直到宇宙的热量死亡。

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.

Firefox似乎对正则表达式有迭代限制,并且在大约一百万次尝试后将中止,Chrome和IE在这里似乎更加坚忍。

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.

这篇关于当使用IE& amp;匹配正则表达式时,javascript会挂起铬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆