On Fri, Oct 10, 2008 at 3:12 AM, Hugh Gibson <[EMAIL PROTECTED]> wrote:
> > *Subject:* [qooxdoo-devel] javascript anomaly with regexp literal?
> > *From:* "Derrell Lipman" <[EMAIL PROTECTED]>
> > *To:* "qooxdoo Development" <qooxdoo-devel@lists.sourceforge.net>
> > *Date:* Thu, 9 Oct 2008 12:36:13 -0400
> >
> > This seems odd and I can't find any documentation stating why it's
> > so...
> >
> > This application has a function which tests a string for containing
> > only digits. It uses a regular expression literal assigned to a
> > local variable in a function. That regular expression, however,
> > appears to be persistent across calls to the function.
>
Hi Hugh,
Thanks for looking at this and especially for the links showing that my
expectations aren't out of whack. :-)
> There's a simpler case which exhibits behaviour that is unexpected:
>
In your "simpler case", I think it's doing exactly the documented behavior,
and demonstrates precisely the reason for the existence of lastIndex. When
a regexp.test() returns true, it sets lastIndex to where it found the first
match that follows the pre-call lastIndex position. Initially, with
lastIndex at 0, it finds the very first match. Using a string which
contains multiple matches, though, you can call regexp.test() again with the
same string and obtain the next match, as shown here:
/*
#require(qx.log.appender.Native)
#require(qx.log.appender.Console)
*/
qx.Class.define("custom.Application",
{
extend : qx.application.Standalone,
members :
{
main: function()
{
this.base(arguments);
var re1 = /\?/g;
this.debug("lastIndex=" + re1.lastIndex);
this.debug("test=" + re1.test("a?a?a?"));
this.debug("lastIndex=" + re1.lastIndex);
this.debug("test=" + re1.test("a?a?a?"));
this.debug("lastIndex=" + re1.lastIndex);
this.debug("test=" + re1.test("a?a?a?"));
this.debug("lastIndex=" + re1.lastIndex);
this.debug("test=" + re1.test("a?a?a?"));
this.debug("lastIndex=" + re1.lastIndex);
}
});
which yields this:
3323ms custom.Application[d]: lastIndex=0
3330ms custom.Application[d]: test=true
3337ms custom.Application[d]: lastIndex=2
3343ms custom.Application[d]: test=true
3349ms custom.Application[d]: lastIndex=4
3355ms custom.Application[d]: test=true
3361ms custom.Application[d]: lastIndex=6
3368ms custom.Application[d]: test=false
3383ms custom.Application[d]: lastIndex=0
Thanks again,
Derrell
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
qooxdoo-devel mailing list
qooxdoo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel