Rob said:
Thomas 'PointedEars' Lahn wrote [...]:
`/.../' is equivalent to `new RegExp(...)', see ECMAScript (ES) 3,
7.8.5. There is a RegExp object created on each call and GC'd shortly
after, so it is more efficient to create that object once and make it
globally available. To avoid spoiling the global namespace and attach
the object reference to the method that uses it, I wrote
JSON.parse = function(...) { ... JSON.parse.rx ... };
JSON.parse.rx = /.../
Thanks for the reference,
Standard ECMA-262 3rd Edition - December 1999
7.8.5 Regular Expression Literals
A regular expression literal is an input element that is converted
to a RegExp object (section 15.10) when it is scanned. The object
is created before evaluation of the containing program or function
begins. Evaluation of the literal produces a reference to that object;
it does not create a new object. ...
The above confirms my "AIUI" above, and confirms that there *isn't*
a "new RegExp object created on each call".
Yes, indeed. Somehow I overlooked the following sentences all the time,
and it appears I was not the only one here. Thank /you/ for pointing that
out.
Has this version (ECMA-262)
It is ECMA-262 (ECMAScript) _Edition_ 3, actually.
There is a PDF and Microsoft Word version of the ECMAScript Language
Specification that have 3 more pages (ref. PDF versions), are titled
"Edition 3 Final" and dated March 24, 2000 inside. (They refer to
themselves being downloadable from ftp.ecma.ch. However, [ftp.]ecma.ch
is no longer and ftp.ecma-international.org appears not to provide
access with anonymous login.)
These can be downloaded from
<URL:
http://www.mozilla.org/js/language/>
Although it does not appear to include the required corrections mentioned
in the errata, the "Final" addition and the date indicate that this is the
latest revision published by the ECMA; it is unclear why only the December
1999 revision is linked on ecma-international.org. (Maybe the mozilla.org
folks have access to more recent information on ECMA's FTP server because
the Mozilla Foundation is an ECMA member.) A text comparison between the
two revisions I did today is inconclusive as yet.
However, whether it should be considered normative or not, that latest
revision says the same as its predecessor; you are correct.
This shouldn't be a problem for a RegExp that only ever has test()
called on it (as with the OP's code) as AFAICT exec() will only
ever reset the lastIndex property to 0 (which is the default anyway).
No, it could pose a problem since the next match will start from the
position the `lastIndex' property indicates. The value of that property is
reset to 0 iff "I < 0 or I > length" (15.10.6.2.6.), where according to
step 2 `length' refers to the length of the string the method is passed.
It is unclear what `I' refers to; known implementations suggest that this
is a typo not covered in the errata and actually `i' is meant. If we
assume this, `i' would be the value of ToInteger(lastIndex), according to
step 4, which is in fact the behavior of those implementations. That means
previous calls of RegExp.prototype.exec() on the same RegExp object do
affect the current call on the same object, unless
| 5. If the global property is false, let i = 0.
According to 15.10.4.1,
| The global property of the newly constructed object is set to a Boolean
| value that is true if F contains the character "g" and false otherwise.
So it does not pose a problem _here_, as Douglas is not using a global
expression (and the expression is anchored on both sides anyway.)
PointedEars