es-discuss  

Re: Inline regexps and caching

David-Sarah Hopwood
Fri, 23 Jan 2009 13:10:30 -0800

Laurens Holst wrote:
> 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.

This is a known problem that has been fixed in ES3.1.

ES3 section 7.8.5:
# A regular expression literal is an input element that is converted to
# a RegExp object (section 15.10) when it is scanned.

ES3.1 section 7.8.5:
# A regular expression literal is an input element that is converted to
# a RegExp object (section 15.10) each time the literal is evaluated.

-- 
David-Sarah Hopwood ⚥

_______________________________________________
Es-discuss mailing list
Es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss