Does the RegExp /y modifier require /g?
On Dec 24, 2007, at 8:44 AM, StevenLevithan wrote:
(I emailed this to the list last night, but it doesn't seem to have
gone through. Sorry if this becomes a dupe.)
(I don't see a moderator request based on your sending from a
different e-mail address -- I don't see a dup either.)
Given the above rule, my questions are:
- Does ES4's /y (sticky) modifier have any meaning if the /g (global) modifier is not also set?
Yes.
- What about with String.prototype.split and
String.prototype.search, which ignore the values of regexp.global and regexp.lastIndex?
The /y flag makes unanchored regexps match or fail at the current
position in the target string, period. Where the current position is
may depend on /g and other things, but /y is independent (lower-
level) than these considerations.
I'm working on code which brings some of the ES4 regex features
(including /y) to current browsers, but I'm not sure which way to go on these
points.
Firefox 3 is in beta release now and has supported /y since an early
alpha. See bugzilla.mozilla.org/show_bug.cgi?id=371932.
- Does ES4's /y (sticky) modifier have any meaning if the /g (global) modifier is not also set?
Yes.
My question was very poorly worded. I was thinking along the lines of behavior being affected by lastIndex (set manually or via the use of /g).
The /y flag makes unanchored regexps match or fail at the current position in the target string, period. Where the current position is may depend on /g and other things, but /y is independent (lower- level) than these considerations. [...] Firefox 3 is in beta release now and has supported /y since an early alpha. See bugzilla.mozilla.org/show_bug.cgi?id=371932.
Thanks. Your response and the Firefox 3 beta answers all my questions on this point. The /y modifier is a great addition and every aspect of it seems to work like I would expect/hope.
(I emailed this to the list last night, but it doesn't seem to have gone through. Sorry if this becomes a dupe.)
As far as I understand from ECMA-262 3rd Edition, the regexp.lastIndex property is meaningless if a regexp does not use the /g modifier. Quoting from E262v3 §15.10.6.2 (RegExp.prototype.exec):
So, e.g., the following returns true, although it would return false if /g were used:
var re = /x/; re.lastIndex = 2; re.test("xyz"); // true
Given the above rule, my questions are:
Does ES4's /y (sticky) modifier have any meaning if the /g (global) modifier is not also set?
What about with String.prototype.split and String.prototype.search, which ignore the values of regexp.global and regexp.lastIndex?
The extend_regexps proposal on the wiki does not specifically mention these points, and the sticky modifier does not appear to be implemented in the ES4 reference implementation (the sticky property gets set with /y, but it doesn't appear to work).
I'm working on code which brings some of the ES4 regex features (including /y) to current browsers, but I'm not sure which way to go on these points.