By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Why does JavaScript Regx behave weirdly?

P: n/a
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?

Sep 22 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Cain <st***********@gmail.comwrites:
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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 22 '08 #2

P: n/a
Cain wrote:
[...]
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:

<48**************@PointedEars.de>
(Fri, 19 Sep 2008 01:50:43 +0200)

<67**********************************@a70g2000hsh. googlegroups.com>
(Mon, 22 Sep 2008 07:15:23 -0700 (PDT))

See also <http://www.jibbering.com/faq/#FAQ2_3>.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Sep 22 '08 #3

P: n/a
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~
Sep 22 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.