Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? (http://www.velocityreviews.com/forums/t928773-regexp-exec-returns-null-when-there-is-a-match-a-javascript-regexp-bug.html)

Uldis Bojars 12-17-2006 07:40 PM

RegExp.exec() returns null when there is a match - a JavaScript RegExp bug?
 
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/ ]


Lee 12-17-2006 09:50 PM

Re: RegExp.exec() returns null when there is a match - a JavaScript RegExp bug?
 
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-...xp.htm#1194735


--


Janwillem Borleffs 12-17-2006 09:59 PM

Re: RegExp.exec() returns null when there is a match - a JavaScript RegExp bug?
 
Uldis Bojars wrote:
> 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





All times are GMT. The time now is 01:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.