473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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/ ]

Dec 17 '06 #1
2 14640
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

Dec 17 '06 #2
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-...xp.htm#1194735
--

Dec 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andrew DeFaria | last post by:
I was reading my O'Reilly JavaScript The Definitive Guide when I came across RegExp and thought I could tighten up my JavaScript code that checks for a valid email address. Why does the following...
5
by: Ronald Fischer | last post by:
I have a server-side JavaScript function returning a string. I would like to test wheather or not the string contains the following pattern: - an equal sign, - followed by one or more characters...
1
by: weston | last post by:
Has anyone ever encountered trouble with regular expressions not capturing matches specified by parentheses? I seem to have a weird situation where a regular expression is matching the String I'm...
2
by: sathya | last post by:
hi, i am using regexp.index in my javascript, IE supports this value but mozilla gives undefined value for regexp.index. Is there any equivalent method we can use for mozilla browsers. ...
3
by: jasonkester | last post by:
Just a heads up for anybody that comes across this in the future. Noticed a strange behavior in RegExp.test() today. Check out the following code. It will alternately display "chokes" and null,...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
11
by: HopfZ | last post by:
I coudn't understand some behavior of RegExp.test function. Example html code: ---------------- <html><head></head><body><script type="text/javascript"> var r = /^https?:\/\//g;...
6
by: runsun pan | last post by:
Hi I am wondering why I couldn't get what I want in the following 3 cases of re: (A) var p=/(+-?+):(+)/g p.exec("style='font-size:12'") -- // expected
4
by: Matt | last post by:
Hello all, I have just discovered (the long way) that using a RegExp object with the 'global' flag set produces inconsistent results when its test() method is executed. I realize that 'global'...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.