“现代”中正则表达式的最大大小/长度。网络浏览器? [英] Maximum size/length of regular expression in "modern" web browsers?
问题描述
现代浏览器(即Firefox 3 +,Safari 4 +,IE 7+)中正则表达式的最大大小是多少?假设一个简单的正则表达式,例如foo | bar | baz | woot | ...
What's the maximum size of a regular expression in modern browsers (i.e. Firefox 3+, Safari 4+, IE 7+)? Assume a simple regular expression, of, say "foo|bar|baz|woot|..."
推荐答案
你可以使用这个代码在IE8 / firefox中使用firebug / Chrome进行测试。
You can use this code to test, in IE8 / firefox with firebug / Chrome.
var regex = "";
var maximum = 100;
var showAfter = 95;
for(i = 1; i < maximum; i++) {
regex += "aaaaaaaaaa";
if (i > showAfter) {
console.log(10 * i + " chars");
console.log(RegExp(regex));
}
}
当您收到错误时,您找到了限制。
When you get a error, you found the limit.
SIMPLE TEST
var regex = "";
var chars = 3204161;
for(i = 0; i < chars; i++) {
regex += "a";
}
alert(chars + " chars");
var a = RegExp(regex); // don't send to console, to be faster
结果
在Firefox 3.6.3(Ubuntu 32位)中,当我尝试使用带有 9M字符的正则表达式时出错( 9.999.990字符) 3.204.161 字符。 3.204.160没关系。
In Firefox 3.6.3 (Ubuntu 32 bits) I get error when I tried a regex with 9M chars (9.999.990 chars) 3.204.161 chars. With 3.204.160 it's ok.
在Chrome 5.0.3中,限制在20M到25M之间。
In Chrome 5.0.3 the limit is something between 20M and 25M chars.
firefox中的错误是:
The error, in firefox, is:
script stack space quota is exhausted
注意:如果你做了一些测试,请在这里评论。
Note: If you did some test, please comment here.
这篇关于“现代”中正则表达式的最大大小/长度。网络浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!