繁体   English   中英

为什么此Javascript match()函数会崩溃我的浏览器?

[英]Why does this Javascript match() function Crash My Browser?

我所做的:

  • 确保Firebug正常运行。
  • 在isWrappedInParens()函数中放置一个断点。
  • 导航到我的Web应用程序。
  • 触发对isWrappedInParens()的调用。
  • 单步执行isWrappedInParens()。 一切正常,但不会越过指示为“ CRASH POINT”的代码行。
  • 我还尝试了在没有运行Firebug或没有断点的情况下进行尝试,但是仍然冻结。

我注意:

  • 在大多数情况下,isWrappedInParens()可以正常工作。
  • 当它不起作用时,Firefox冻结。 不过,我仍然可以最小化/扩展/关闭窗口。
  • 我还注意到,当测试字符串较短(括号较少)时,Firefox会挂起,但最终正确完成(约30秒)。

导致FIREFOX崩溃的示例字符串

// Note that this is not wrapped in parentheses,
// since it is two separate sets of nested parentheses
var test = "(the OR (and) OR (and) OR (and)) AND ((to) OR (to) OR (to))";

背景

  • 浏览器:Firefox 3.6.18
  • 该webapp是一个码头应用程序。

isWrappedInParens = function(str){
    if(_.isNull(str)) {
        return false;
    }
    str = str.trim();

    var pattern = /^[(](([(][^()]+[)]|[^()]+)|[(]([(][^()]+[)]|[^()]+)+[)])+[)]$/;

    var matchesPattern;
    try{
        matchesPattern = str.match(pattern) || null; //CRASH POINT!!!!!!!!!!!
    }catch(err){
        return false; //Note that no error is ever caught from freezing
    }

    var isWrapped = !_.isUndefined(matchesPattern) && !_.isNull(matchesPattern);
    return isWrapped;
}

正则表达式来自哪里:

// Atoms, building blocks for the expressions
var parenAtom = "[(][^()]+[)]";
var nonParenAtom = "[^()]+";

// Expressions, building blocks for the final regular expression
var baseCase = "(" + parenAtom + "|" + nonParenAtom + ")";
var nestedCase = "[(]_base_[)]"
    .replace("_base_", baseCase);

// Regular Expression
var wholeCase = "^[(](_base_|_nested_)+[)]$"
    .replace("_base_", baseCase)
    .replace("_nested_", nestedCase);
var pattern = new RegExp(wholeCase, "");

从我的评论:

在Firefox错误数据库中查看,发现了许多正则表达式错误,并将它们粗略地分类为“指数行为”错误。 其中大多数已在较新版本的浏览器中修复。

在此错误中,Brendan Eich对此问题发表了一些评论,他列出了其他几个错误(有些很旧)。 那里的另一条评论暗示了Firefox 4中的“正则表达式大修”,表明早在那之前发生了许多更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM