Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum size/length of regular expression in "modern" web browsers?

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|..."

like image 372
Tyson Avatar asked Sep 10 '10 00:09

Tyson


People also ask

What is regular expression in web technology?

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.

How do you limit a length in regex?

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.

How browsers function at a high level?

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.

How do modern browsers work?

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.


2 Answers

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.

like image 96
Topera Avatar answered Oct 16 '22 09:10

Topera


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.

like image 41
Chris Avatar answered Oct 16 '22 10:10

Chris