Updates:
Summary: JavaScript regular expression fails to match date
Cc: [EMAIL PROTECTED] [EMAIL PROTECTED]
Comment #8 on issue 2161 by [EMAIL PROTECTED]: JavaScript regular
expression fails to match date
http://code.google.com/p/chromium/issues/detail?id=2161
Sadly, I think this is working as intended, due to some quirky behavior
defined in
the ECMA 262 spec. I have CC'ed some real JavaScript experts (unlike
myself) to
verify my understanding.
Lengthy explanation follows, only read if you're interested in the ECMA
specification, otherwise skip ahead to the "Potential workarounds" section
below.
----------------------------------------
Looking at the reduced test case, there is a regular expression literal on
line 5.
According to the spec, there is only one instance of a regular expression
literal
(ECMA 262 7.8.5), which is created when the literal is scanned. So each
time jsfGetFormDate is called, a new RegExp object is not created. Instead
the same
RegExp object is reused over and over again.
Since the 'g' flag is specified, that means that the global property is
true for the
RegExp object (ECMA 262 15.10.4.1). In the case where the global property
is true,
when exec successfully finds a match, the lastIndex property of the RegExp
is set to one plus the index of the last input character matched so far
(ECMA 262 15.10.2.1,
15.10.6.2 step 11). So in this case lastIndex is 10 after the first call
to jsfGetFormDate, since the length of the input string is 10 and the
complete string is
matched.
Remember that there is only one instance of the regular expression literal
RegExp
object, so the next time jsfGetFormDate is called, the lastIndex property
of the
RegExp object is still 10. So the next time exec is called, it attempts to
match
starting at index 10 of the input string (ECMA 262 15.10.6.2 step 7).
Since the
input string is only 10 characters in length, no match is found. Then it
tries again
starting at index 11 (ECMA 262 15.10.6.2 steps 8 and 9) and, because 11 is
greater
than the length of the input string (which is 10), it fails and returns
null (ECMA
262 15.10.6.2 step 6).
----------------------------------------
Potential workarounds:
1. Do not specify the global 'g' flag in the regular expression.
2. Instead of using a regular expression literal, instead use something
like this:
var p = '(0?\\d{1,2})[\\/-]0?\\d{1,2}[\\/-]\\d{4}';
var re = new RegExp(p, 'g');
which will create a new RegExp object each time.
--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Chromium-bugs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/chromium-bugs?hl=en
-~----------~----~----~----~------~----~------~--~---