Inline regexps and caching

Laurens Holst lholst at students.cs.uu.nl
Fri Jan 23 03:34:02 PST 2009


Hi,

I and a colleague were puzzled by some strange behaviour in Firefox, we 
found that in some browsers literal regular expressions are cached and 
reused. Testcase:

function test(str) {
    var regexp = /^[^d]*\bd{1,4}\b[^d]*$/g;
    alert('Expexted: 0/true, result: ' +
            regexp.lastIndex + '/' +
            regexp.test(str)
        );
}

var xxx = "MM/dd/yyyy";
test(xxx);
test(xxx);

It turns out that Firefox and Opera return ‘false’ for the second test 
result, whereas Internet Explorer and Safari return ‘true’ in both cases.

The latter behaviour seems most sensible and expected to me; browsers 
can of course cache the regular expression object to avoid parsing it 
over and over again, but they should IMO clone that cached object every 
time it is used.

~Laurens

-- 
Note: New email address! Please update your address book.

~~ Ushiko-san! Kimi wa doushite, Ushiko-san nan da!! ~~
Laurens Holst, student, university of Utrecht, the Netherlands
Website: www.grauw.nl. Backbase employee; www.backbase.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: lholst.vcf
Type: text/x-vcard
Size: 134 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20090123/3c1e696d/attachment.vcf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3265 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20090123/3c1e696d/attachment.bin>


More information about the Es-discuss mailing list