RegExp.exec() returns null when there is a match - a JavaScript RegExp bug?

U

Uldis Bojars

Hi All,

I have encountered problems with JS RegExp.exec() and can't find what
is the problem. Could you help me?

formRequest is a function that extracts some information from
XMLHTTPRequest response. A very strange effect (and I can't find where
I've done something wrong) is that regexp matches in this function fail
on every second call.

Contents of the 'response' variable are similar in all calls and I
tested with javascript regexp evaluator that there is a match for these
regexped in the response text. Nevertheless JS returns null.

function formRequest( response )
{

var re = /<input type=hidden name="([^"]*)" value="([^"]*)">/g;
var m2 = re.exec( response );
alert(m2);

var re = /<input src="([^"]*)" border="([^"]*)"
height="([^"]*)" type="image" width="([^"]*)" name="([^"]*)"
value="([^"]*)">/g;
var m3 = re.exec( response );
alert(m3);

}

A simple workaround is to check if exec() returns null and do the
request again:
if (m2 == null)
m2 = re.exec( response );
This helps and then the problem shifts to "var m3 = re.exec( response
);" which can be fixed in a similar manner.

While this fixes the problem somehow it is not acceptable that a
program (or JavaScript?) behaves in this seemingly unrational way and
I'd like to get it working properly.

P.S. Script is being executed from a XUL extension in Mozilla Firefox
1.5.

Thanks,
Uldis

[ http://captsolo.net/info/ ]
 
L

Lee

Uldis Bojars said:
Hi All,

I have encountered problems with JS RegExp.exec() and can't find what
is the problem. Could you help me?

TFM says:

If the match succeeds, the exec method returns an array and
updates properties of the regular expression object and the
predefined regular expression object, RegExp. If the match
fails, the exec method returns null.

If your regular expression uses the "g" flag, you can use the
exec method multiple times to find successive matches in the
same string. When you do so, the search starts at the substring
of str specified by the regular expression's lastIndex property.

http://docs.sun.com/source/816-6408-10/regexp.htm#1194735


--
 
J

Janwillem Borleffs

Uldis said:
A simple workaround is to check if exec() returns null and do the
request again:
if (m2 == null)
m2 = re.exec( response );
This helps and then the problem shifts to "var m3 = re.exec( response
);" which can be fixed in a similar manner.

The exec() method stores the result indexes in the RegExp.lastIndex property
and starts searching from that point for future uses. Resetting the property
to 0 solves your problem.

Example:

var what = 'dabcd';

var re = /a/g;
var m2 = re.exec(what);
alert(m2); // alerts a

var m3 = re.exec(what);
alert(m3); // alerts null

re.lastIndex = 0;
var m3 = re.exec(what);
alert(m3); // alerts a


JW
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top