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|..."
A regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.
By combining the interval quantifier with the surrounding start- and end-of-string anchors, the regex will fail to match if the subject text's length falls outside the desired range.
The browser's high level structure #The browser engine: marshals actions between the UI and the rendering engine. The rendering engine: responsible for displaying requested content. For example if the requested content is HTML, the rendering engine parses HTML and CSS, and displays the parsed content on the screen.
As a client/server model, the browser is the client run on a computer that contacts the Web server and requests information. The Web server sends the information back to the Web browser which displays the results on the computer or other Internet-enabled device that supports a browser.
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
RESULTS
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.
In Chrome 5.0.3 the limit is something between 20M and 25M chars.
The error, in firefox, is:
script stack space quota is exhausted
Note: If you did some test, please comment here.
Certain regular expressions require exponential amounts of memory to evaluate. Since Firefox does this on the stack, which is limited to 10 MB on many Linux distributions, and even smaller in Windows (at least some versions of Firefox), you could hit the limit fairly quickly if you use a regular expression that requires exponential memory to convert to DFA form to evaluate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With