Inconsistent regex results

G

Gary Wardell

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("(e-mail address removed)") != 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
 
M

Martin Honnen

Gary said:
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("(e-mail address removed)") != 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.
 
B

Bjoern Hoehrmann

* 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("(e-mail address removed)") != 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.
 
E

Evertjan.

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("(e-mail address removed)") != 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('(e-mail address removed)') )
alert('Match')
else
alert('No match');
 
G

Gary Wardell

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
 
M

Martin Honnen

Gary said:
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.
 
G

Gary Wardell

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
 

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

Top