String.split() and backtracking in IE7

handgestrickt hoped that the Microsoft-team that is responsible for the JScript-Engine fixed the backtracking- or parenthesis- bug in Internet Explorer 7. if you use a regular expression with parenthesis in String.split() in Internet Explorer, the matches for the parenthesized expressions are not returned in the resulting array. this is very annoying, especially when you want to write good parsers in JavaScript. we wanted to write an HTMLbeautifier, so we had to parse HTML quick and dirty. we will release this beautifier later. here is the fix for the annoying bug. feel free to use it.

var foo = {
        splitFix: function(string,regexp) {
                var matches = [];
                if(!document.all || window.opera) matches = string.split(regexp);
                else {
                        var delim = '<#%fuckIE!>'; //lol
                        var prnth = String(regexp).replace(/\(\?/g,'');
                        prnth = prnth.replace(/[^\(]+/g,'').length;
                        var repl = delim;
                        for(var a=0;a<prnth;a++) repl += '$<span>'</span>+(a+1)+delim;
                        eval("string = string.replace("+regexp+"g,repl);");
                        matches = string.split(delim);
                }
                return matches;
        }
}

Saturday, 17. March 2007 • trackback url

Add Comment

( to reply to a comment, click the reply link next to the comment )

Comment Title:
Your Name:
Email Address:
Make Public?
Website:
Make Public?

Comment:


Allowed XHTML tags : a, b, i, strong, code, acrynom, blockquote, abbr. Linebreaks will be converted automatically.


Captcha:

captcha image

please type the content of the above image into the following form-field: