RegExp doesn't capture matches in parentheses?

W

weston

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 running
it against, but none of the parenthized expressions are coming back in
the array.

I'm basically trying to pull a topic out of a bulletin board page. So
I'll run:

var bodystr = new
String(document.getElementsByTagName('body')[0].innerHTML)
;
var topicPattern = /<p style="margin-left: 4px;"><b><font
face="Arial" size=
"2">(.*?)<\/font><\/b><\/p>/ig;
var matches = bodystr.match(topicPattern);

And here's the weird thing. Apparently, matches[0] is filled, with the
string that matches the whole pattern....

But there's nothing in matches[1]. Also nothing in RegExp.$1.

Am I misunderstanding how this is supposed to work, or is there
something genuinely odd about this?

If anyone wants to see this in context, here's an example:

http://mmedia.csoft.net/LE/testpage.html

Click the link in the upperhand corner to see it in action. Feel free
to poke around that directory, too.
 
M

Michael Winter

Has anyone ever encountered trouble with regular expressions not
capturing matches specified by parentheses?

No. :)

[snip]
var bodystr = new
String(document.getElementsByTagName('body')[0].innerHTML)
;

Creating a String object isn't really necessary unless you are going to
call multiple methods on that object.
var topicPattern = /<p style="margin-left: 4px;"><b><font
face="Arial" size="2">(.*?)<\/font><\/b><\/p>/ig;

I don't really recommend using non-greedy quantifiers as some older
browsers will think them syntax errors. I'd use

([^<]*)

as you're just trying to avoid matching the next tag.
var matches = bodystr.match(topicPattern);

And here's the weird thing. Apparently, matches[0] is filled, with the
string that matches the whole pattern....

But there's nothing in matches[1]. Also nothing in RegExp.$1.

All is as it should be. When the match method is called with a regular
expression that has its global flag set, it calls the exec method of
that regular expression until no more matches can be found, and returns
an array containing the first element (the matching sub-string) from
each of the exec method results.

Call the exec method instead:

match = topicPattern.exec(bodystr);

The second element (matches[1]) will contain the captured value.

[snip]
http://mmedia.csoft.net/LE/testpage.html

Click the link in the upperhand corner to see it in action. Feel free
to poke around that directory, too.

All I find is a lot of script errors, and not much more.

Mike
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top