> *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.

There's a simpler case which exhibits behaviour that is unexpected:

var re1 = /\?/g;
re1.lastIndex;
0

re1.test("a?");
true

re1.lastIndex;
2

re1.test("a?aaa");
false

re1.lastIndex;
0

re1.test("a?");
true

re1.lastIndex;
2

"a?aa".replace(re1, "b");
"abaa"

re1.lastIndex;
0

This was collated from various tests in Firebug console. So calling test()
using a regular expression changes lastIndex to after the match position
(or resets it to 0 if no match), but calling replace() resets it to 0
before and after the replace action.

So you have to be careful using test() quite apart from the fact that the
same regular expression object is used when a regex literal is declared.

Hugh

-------------------------------------------------------------------------
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

Reply via email to