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

Inconsistent regex results

P: n/a
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
Share this Question
Share on Google+
6 Replies


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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.