Why does JavaScript Regx behave weirdly?

C

Cain

A very simple regular expression test:

var regx = /^\d+$/g;
alert(regx.test("11") + ": " + regx.test("11"));

And the output is "true: false".

If I change to regx = new RegExp("^\\d+$", "g");
Then the output is the same.

This is the first weird part.

The second weird part is, when I only test the string once in a
function:

var regx = /^\d+$/g;
alert(regx.test("11") + ": ");

var regx = new RegExp("^\\d+$", "g");
alert(regx.test("11") + ": ");

If I call the function twice, the output will be

1st call:
true, true

2nd call:
true, false

My assumption is the global expression holds an internal status, but
that doesn't explain the second example.

Can someone help me out here please?
 
L

Lasse Reichstein Nielsen

Cain said:
A very simple regular expression test:

var regx = /^\d+$/g;
alert(regx.test("11") + ": " + regx.test("11"));

And the output is "true: false".

Expected behavior. When you use the "g" flag, the regexp remembers the
position end of its match, and tries to match from that point when its
"test" or "exec" methods are next called. It does so until it fails to
match, at which point it resets itself so the next invocation will start
at position 0 again.

The second weird part is, when I only test the string once in a
function:

var regx = /^\d+$/g;
alert(regx.test("11") + ": ");

var regx = new RegExp("^\\d+$", "g");
alert(regx.test("11") + ": ");

If I call the function twice, the output will be

1st call:
true, true

2nd call:
true, false

Your output doesn't match what you wrote above.

Anyway: A regexp literal (/whatnot/) creates only one RegExp instance,
even if it occurs inside a function. Not once each time the function
is called. Since the regexp literal has the "g" flag, the same case
as above occurs.

My assumption is the global expression holds an internal status, but
that doesn't explain the second example.

It does :)

/L
 
T

Thomas 'PointedEars' Lahn

Cain said:
[...]
var regx = /^\d+$/g;
alert(regx.test("11") + ": " + regx.test("11"));

And the output is "true: false".
[...] when I only test the string once in a function:

var regx = /^\d+$/g;
alert(regx.test("11") + ": ");

var regx = new RegExp("^\\d+$", "g");
alert(regx.test("11") + ": ");

If I call the function twice, the output will be

1st call:
true, true

2nd call:
true, false
[...]

Evidently, you have not been paying attention:

<[email protected]>
(Fri, 19 Sep 2008 01:50:43 +0200)

<672deda4-6678-4abb-b8f4-a5f4d714775f@a70g2000hsh.googlegroups.com>
(Mon, 22 Sep 2008 07:15:23 -0700 (PDT))

See also <http://www.jibbering.com/faq/#FAQ2_3>.


PointedEars
 
C

Cain

Thank you for the wonderful explanation:)
I have been struggling on this for so many days, and couldn't find a
reasonable explanation on the internet.
Thanks a million~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top