Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > RegExp.exec() returns null when there is a match - a JavaScript RegExp bug?

Reply
Thread Tools

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

 
 
Uldis Bojars
Guest
Posts: n/a
 
      12-17-2006
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/ ]

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      12-17-2006
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


--

 
Reply With Quote
 
 
 
 
Janwillem Borleffs
Guest
Posts: n/a
 
      12-17-2006
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
Begin() applied on empty Vector returns NULL or non null value???? Col C++ 1 04-21-2006 01:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57