473,513 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inconsistent regex results

Hi,

I wanted to check if this was correct be behavior before reporting it as a
bug in FireFox.

Given this function:

function test()
{
var re;

re = /^(.+)\@(.+?)$/gi;
if (re.exec("te**@example.com") != null)
{
alert("Match");
} else {
alert("Null");
}

return;
}

Should it alternately between displaying "Match" and "Null" each time it's
called?

I would think that since the regex and the string are always the same if
should always display "Match"???

What I am seeing is that it alternates.

If this is proper behavior, why?

Gary
Jun 27 '08 #1
6 1478
Gary Wardell wrote:
Should it alternately between displaying "Match" and "Null" each time it's
called?

I would think that since the regex and the string are always the same if
should always display "Match"???

What I am seeing is that it alternates.

If this is proper behavior, why?
Use

function test()
{
var re;

re = new RegExp('^(.+)\\@(.+?)$', 'gi');
if (re.exec("te**@example.com") != null)
{
alert("Match");
} else {
alert("Null");
}

return;
}

or live with the ECMAScript specification insisting on creating one
regular expression object for each regular expression literal, even if
it is inside of a function as a local variable:
"7.8.5 Regular Expression Literals
A regular expression literal is an input element that is converted to a
RegExp object (section 15.10) when it is
scanned. The object is created before evaluation of the containing
program or function begins. Evaluation of the
literal produces a reference to that object; it does not create a new
object."

So what happens is that in your code the regular expression literal
creates one regular expression object with the g flag when the program
code is scanned, and then when the function is executed that regular
expression object is referenced and because of the g flag every second
exec starts after the last match and does not find anything.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
* Gary Wardell wrote in comp.lang.javascript:
>I wanted to check if this was correct be behavior before reporting it as a
bug in FireFox.

Given this function:

function test()
{
var re;

re = /^(.+)\@(.+?)$/gi;
if (re.exec("te**@example.com") != null)
{
alert("Match");
} else {
alert("Null");
}

return;
}

Should it alternately between displaying "Match" and "Null" each time it's
called?
A quick reading of ECMA-262 15.10.6.2 suggests just that, yes. Note that
you are using the 'global' ('g') property so; if you don't, you get the
result you expect.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jun 27 '08 #3
Gary Wardell wrote on 24 apr 2008 in comp.lang.javascript:
Hi,

I wanted to check if this was correct be behavior before reporting it
as a bug in FireFox.

Given this function:

function test()
{
var re;

re = /^(.+)\@(.+?)$/gi;
if (re.exec("te**@example.com") != null)
{
alert("Match");
} else {
alert("Null");
}

return;
}

Should it alternately between displaying "Match" and "Null" each time
it's called?

I would think that since the regex and the string are always the same
if should always display "Match"???

What I am seeing is that it alternates.

If this is proper behavior, why?
1 You should use .test() to test, that's where test() is for.

2 in exec() the ponter of the defined regex object is kept from the last
call, this could be your problem.
re = /^(.+)\@(.+?)$/gi;
3 @ does not need to be escaped,
the () are unneccessary,
the g flag is not used,
the i flag is not used.

Try:

if ( /^.+@.+$/.test('te**@example.com') )
alert('Match')
else
alert('No match');


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jun 27 '08 #4
Hi Martin,

Thanks for the explanation.

Given the regex ^(.+)\@(.+?)$ I'm wondering if a really need the "gi",
since it's matching the whole string and it doesn't really care about case?

Is "new" the preferred way to create regex objects or is it just better in
this case?

I'm a little bit new to both regex and JavaScript.

Gary
Jun 27 '08 #5
Gary Wardell wrote:
Given the regex ^(.+)\@(.+?)$ I'm wondering if a really need the "gi",
since it's matching the whole string and it doesn't really care about case?

Is "new" the preferred way to create regex objects or is it just better in
this case?
No, you can use regular expression literals but you need to be aware of
the consequences. In your sample you could drop the g flag and also, as
already suggested, use the test method instead of the exec method.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 27 '08 #6
Hi,

Yes, I came across that in looking for why it wasn't working.

However, exec returns the captured strings in an array and I need to have
the account separated from the domain name.

I left that out to simplify the sample code.

It's working now.

Thanks,

Gary
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1861
by: Lord Khaos | last post by:
If I am trying to find an expression, foo, I can do something like this: rExp = /foo/gi; if(results.search(rExp) > -1){ and all work fine. however, if I want my search term to be a variable, bar: var bar= "foo";
7
5712
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning is to improve, but not to prove.
5
310
by: Fritz Switzer | last post by:
I've got some strings I'd like to regex.split. Any ideas on what the format would be for these examples. I'm webscraping so I have no control on the inputs. A couple points: the POS can be both single and two characters G C F SG PF PG etc. NO can be 1 or 2 digits. The WT would always be 100-999 pounds. NO PLAYER POS HT WT...
6
4775
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or...
20
8027
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
4
3831
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
15
50147
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
10
1849
by: bullockbefriending bard | last post by:
first, regex part: I am new to regexes and have come up with the following expression: ((1|),(1|)/){5}(1|),(1|) to exactly match strings which look like this: 1,2/3,4/5,6/7,8/9,10/11,12 i.e. 6 comma-delimited pairs of integer numbers separated by the
4
1934
by: Flomo Togba Kwele | last post by:
I am having difficulty writing a Regex constructor. A line has a quote(") at its beginning and its end. I need to strip both characters off. If the line looks like "1", I need the result to be 1. If it were sed, I could just do s/^"// and s/"$//, but I'm confused with the quote escape characters. Also, can I do both replacements at once? ...
0
7269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7177
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7394
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.