[英]Why does this Javascript match() function Crash My Browser?
// 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))";
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.