Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2012, 08:35 AM   PM User | #1
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
regex test problem

How do you explain that ?

Code:
regexPattern = new RegExp("a","gi");
s = "a";
        alert (regexPattern.test(s));//--> true
        alert (regexPattern.test(s));//--> false
        alert (regexPattern.test(s));//--> true
        alert (regexPattern.test(s));//--> false
        //......
EDIT: if I modify each alert:
Code:
 alert (regexPattern.test(s) + s.match(regexPattern));
obviously result of
Code:
 s.match(regexPattern)
is the same on each alert.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search

Last edited by BubikolRamios; 12-09-2012 at 09:26 AM..
BubikolRamios is offline   Reply With Quote
Old 12-09-2012, 10:54 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Hmm. Very odd! You seem to have exposed a bug (not a flower! ) in the Javascript regex engine. Same in IE and Chrome.

Code:
var s = "a";
alert (/a/gi.test(s)); // true
alert (/a/gi.test(s)); // true
alert (/a/gi.test(s)); // true
alert (/a/gi.test(s)); // true

"If you make it idiot proof, they'll build a better idiot"
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-09-2012, 11:12 AM   PM User | #3
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
Quote:
Found a flower or bug
Bugs also there (-:

Yeah, tested the thing in chrome, before submiting here.
Not to mention hours lost, before 'lamp' turned on: "Damn, there something smell bad about .test".
I had .test in a loop and "a" string has been changing and on every second loop bad result.
__________________
Found a flower or bug and don't know what it is ?
agrozoo.net galery
if you don't spot search button at once, there is search form:
agrozoo.net galery search

Last edited by BubikolRamios; 12-09-2012 at 11:21 AM..
BubikolRamios is offline   Reply With Quote
Old 12-09-2012, 11:14 AM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.
Mozilla
Code:
regexPattern = new RegExp("a","gi");
s = "a";
        alert (regexPattern.test(s));//--> true
        alert(regexPattern.lastIndex); // 1
        alert (regexPattern.test(s));//--> false
        alert(regexPattern.lastIndex); // 0
        alert (regexPattern.test(s));//--> true
        alert(regexPattern.lastIndex); // 1
        alert (regexPattern.test(s));//--> false
        alert(regexPattern.lastIndex); // 0
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-09-2012, 11:25 AM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
If you remove the "g" global modifier - which is not relevant to test() - then it behaves as expected.

Added: I won't have JavaScript besmirched
(only joshing!)
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-09-2012 at 11:27 AM..
AndrewGSW is offline   Reply With Quote
Old 12-09-2012, 11:28 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by AndrewGSW View Post
If you remove the "g" global modifier - which is not relevant to test() - then it behaves as expected.
You are right! Woot!
We learn something every day. Sometimes something worth knowing!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 12-09-2012, 12:17 PM   PM User | #7
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
It is the same thing with prompt or confirm and if an "a" take place of s !

In short (See this MDN page *) you have to restore the regexPattern.lastIndex to 0 before each new test !

Good to know. Thanks !

(*) Or better ECMAScript Language Specification 15.10.6.2 and 15.10.6.3
007julien is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:18 AM.


Advertisement
Log in to turn off these ads.