Maximum size / length of regex in "modern" web browsers?

What is the maximum regex size in modern browsers (i.e. Firefox 3+, Safari 4+, IE 7+)? Suppose a simple regular expression, for example, "foo | bar | baz | woot | ..."

+5
source share
3 answers

This code can be used for testing 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 receive an error message, you have found a restriction.


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 an error when I tried a regex with 9M characters (9.999.990 characters) 3.204.161 . With 3.204.160 this is normal.

Chrome 5.0.3 - - 20M 25M.

firefox:

script stack space quota is exhausted

. - , .

+9

. Firefox , 10 Linux, Windows ( , Firefox), , , DFA .

+2

, , :

var input = "woot";

var tests = ["foo", "bar", "baz", "woot"];
for(i = 0; i < tests.length; i++) {
   if (tests[i] == input) {
      alert("match found: #" + i);
      break;
   }
}

Then you don’t need to worry about browser restrictions, and as a result, it will most likely be much better (since for the regular expression version you will need to parse and compile the regular expression, there will be a lot of backtracking, etc.).

-1
source

All Articles