473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why does JavaScript Regx behave weirdly?

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
3 1616
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andrea | last post by:
Hi everyone, I am in the process of learning javascript and have a question on location.href. Does javascript always require the <script language = "javascript"> (or script...
11
by: Saqib Ali | last post by:
Please excuse me, this is a fairly involved question that will likely require you to save the file below to a file and open it in a browser. I use Mozilla 1.5, so the problem I describe below...
3
by: Dietmar Prantner | last post by:
Hello, I tried a Javascript on my homepage that works fine on Netscape, works nearlyx normal on Opera but does not work at all on Internet Explorer. Is there anybody out there who can help? I...
19
by: tnhoe | last post by:
Hi, any excel like grid javascript which is stable and fast to load thousand of rows data ? can it have F2 function to popup another window ? can validate cell value when cursor move away that...
6
by: luckyjameel | last post by:
Hi Guys Hope this message shall find u all in gr8 moods. As i was developing a Application for Online Exam and i am in need of a way to maintain the time for the logged in user. So Can any one...
1
by: Glenn Wilson | last post by:
Can anyone help me with a regx to match the values 1 to 6. No these could come in several formats and we only want matches where there is a single number with no other numbers next to it in the...
6
by: Jake Barnes | last post by:
This function has always worked for me just fine: function nl2br_js(myString){ // 02-18-06 - this function imitates the PHP command nl2br, which finds newlines in a string // and replaces them...
1
by: jain236 | last post by:
Hi i am trying to write regx for the time format HH:MM:SS in the below way /\d:\d0:\d/ but i could not get the desired output, can some one throw some light why and where it is going wrong?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.