Inline regexps and caching

Mike Shaver mike.shaver at gmail.com
Fri Jan 23 07:33:10 PST 2009


2009/1/23 Laurens Holst <lholst at students.cs.uu.nl>:
> 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.

Firefox and Opera are doing what ES3 requires (s 7.8.5:
http://bclary.com/2004/11/07/#a-7.8.5 ), but I believe that it's being
changed in 3.1 to produce a new one each time the literal expression
is executed.

Mike


More information about the Es-discuss mailing list