Showing posts with label Regular expressions. Show all posts
Showing posts with label Regular expressions. Show all posts

11.08.2009

Troubles with regular expressions in Opera and Firefox

Several months ago I've found that there is a trouble in Firefox and Opera with regular expressions create by /..../ syntax. Here is an example:
function fn1(template, name){
    var patt = /\d/g;
    var result = null;
    while ((result = patt.exec(template)) != null) {
        fn2();
        document.write(i + ":" + name + " - ", result, "<hr\>");
    }
}

var isCalledOnce = false;

function fn2(){
    if (isCalledOnce) return;
    
    isCalledOnce = true;
    fn1("2,3", "fn2");
}

fn1("0,1", "fn1");
Expected output of this code is:
fn2 - 2
fn2 - 3
fn1 - 0
fn1 - 1
But actual output is:
fn2 - 3
fn1 - 0
fn1 - 0
fn1 - 1
It is not correct. But if you replace line 2:
var patt = /\d/g;
by:
var patt = new RegEx("\d", "g");
All works fine. It seems that regex variable "patt" declared in "fn1" confusing FF and Opera.