Closed
Bug 471303
Opened 17 years ago
Closed 16 years ago
RegExp.$1 is NAN
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
VERIFIED
INVALID
People
(Reporter: tinygirl505, Unassigned)
References
()
Details
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5
The javascript in the listed url produces a NAN error the first
(and only the first) time through its loop. You can see, most
of the code is commented out, but the line that actually does
something performs a mod on RegExp.$1 (in order to convert it to
a 12 hour clock). Again, only _the first time_ through the loop,
FireFox produces NAN as the value for the hour.
[Yes, I recognize the use of innerHTML...]
Reproducible: Always
Steps to Reproduce:
1. just view the url: http://www.greasecab.com/clock.html
2. look closely; you have only _one second_ to see the error
3. reload the page - the NAN error happens every first time through the loop
Actual Results:
Again, you will see - while viewing the page - that the characters
displayed in the 'hour' part of the clock say 'NAN'
After the first second, the clock displays 'numbers' (which is to say
characters that are digits).
Expected Results:
Hmm, well, I suppose what _should_ happen is 'the same thing every time.'
What happens the first time through the loop ought to produce the same
results as the second time.
Once more, there is, of course, use of innerHTML, and the code is
pretty terse. It's not the kind of thing you're going to see from
a lot of javascript writers. I don't believe any of that has any
bearing on the validity of this as a bug. There's an easy work around,
but the question is in how FireFox implements RegExp in javascript.
Updated•17 years ago
|
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Comment 2•16 years ago
|
||
will do.
Comment 3•16 years ago
|
||
This is the result of a bug in the web site's code. The relevant snippet is:
new Date().toString().split(' ')[i].replace(/(^\d\d)/,((eval(RegExp.$1)+23)%12+1));
which, reduced to its component parts is
"11".replace(/(^\d\d)/, RegExp.$1)
But note that RegExp.$1 is a parameter to the replace function! So it is evaluated *before* the call to replace. So on the first iteration, RegExp.$1 is empty, leading to NaN. But on the second iteration, it has the value from the execution of the *first* regexp and things appear to work.
One fix would be to use the lambda form of replace:
"11".replace(/(^\d\d)/, function() { return RegExp.$1 })
Status: UNCONFIRMED → RESOLVED
Closed: 16 years ago
Resolution: --- → INVALID
Comment 4•16 years ago
|
||
tricky. I was about to miss that. thanks mrbkap!
Status: RESOLVED → VERIFIED
Comment 5•16 years ago
|
||
(In reply to comment #3)
> One fix would be to use the lambda form of replace:
>
> "11".replace(/(^\d\d)/, function() { return RegExp.$1 })
Please don't use RegExp.$anything -- rewrite the above to:
"11".replace(/(^\d\d)/, function($_, $1) { return $1; })
The parameter names could be match and group, I just used cute Perl-isms that work in JS.
/be
Updated•16 years ago
|
Flags: blocking1.9.1?
why should I not use RegExp.$anything? That's not something I just made up
out of thin air - are $vars not a standard part of javascript? If you look
at the original script in the page, RegExp.$1 is referred to twice in that
line - the first time to mod it to the 12 hour clock, but then later on in
the same line RegExp.$1 is compared to 12 to write out AM/PM - so, using
this function/return thing, how does one refer to RegExp.$1 further along
in the same line, without using $1?
I'm not trying to use this as a forum for exchanging javascripting tips,
I'm just wondering if my understanding of RegExp could possibly be so off
from what seems like an obvious and parallel understanding.
Also, I don't think it would be possible for me to stress enough how
much I appreciate the work that you do on firefox, and specifically now
much I appreciate your looking into this issue!
Comment 7•16 years ago
|
||
(In reply to comment #6)
> why should I not use RegExp.$anything? That's not something I just made up
> out of thin air - are $vars not a standard part of javascript?
No, RegExp.$1 etc. are not a standard part of JavaScript (that is, they're not in any ECMA-262 edition).
> If you look
> at the original script in the page, RegExp.$1 is referred to twice in that
> line - the first time to mod it to the 12 hour clock, but then later on in
> the same line RegExp.$1 is compared to 12 to write out AM/PM - so, using
> this function/return thing, how does one refer to RegExp.$1 further along
> in the same line, without using $1?
You could save the lambda-function parameter's value in an outer variable.
> I'm not trying to use this as a forum for exchanging javascripting tips,
> I'm just wondering if my understanding of RegExp could possibly be so off
> from what seems like an obvious and parallel understanding.
No problem; the lack of standardization may not be relevant if RegExp.$1 is used a lot, but so far we (members of Ecma TC39) have assumed these old forms are not used enough to require standardization. That may be a bad ass-u-mption ;-) since AFAIK all competitive browsers have to support RegExp.$1 in spite of lack of a de-jure standard.
> Also, I don't think it would be possible for me to stress enough how
> much I appreciate the work that you do on firefox, and specifically now
> much I appreciate your looking into this issue!
Thanks!
/be
You need to log in
before you can comment on or make changes to this bug.
Description
•