...

View Full Version : Regular expressions that can match multiple substrings



NanakiXIII
08-21-2008, 09:47 PM
I'm trying to use regular expressions to filter information out of an HTML table. For example, I might want to strip a row from the table with a certain class, so I used the following piece of code.



var = var.replace(/<tr class="some class">.*<\/tr>/, "");


However, since this is a table it's filled with </tr> tags and the replace function prefers to just continue on until the last </tr> it can find and subsequently delete the entire table. Is there any way I can have the replacement only act upon the row I want, that is, can I tell replace to use the first </tr> tag it comes across, instead of the last one?

rnd me
08-21-2008, 10:42 PM
you can make it ungreedy by using the "?"

exmaple:

var = var.replace(/<tr class="some class">.*?<\/tr>/, "");

NanakiXIII
08-22-2008, 10:58 AM
Thanks, that works perfectly. However, I fail to see exactly why it works. The question mark makes it so that only on instance of .* is searched for, I presume, but I don't see how that relates to my problem. That one instance could still be the entire table, could it not? Would you care to elaborate?

Philip M
08-22-2008, 11:33 AM
var = var.replace(/<tr class="some class">.*<\/tr>/, "");

. means any character
* means zero or more instances of preceding character

so the regex grabs everthing between the first <tr class .....
and the last </\tr>. This is called "greedy" matching.

var = var.replace(/<tr class="some class">.*?<\/tr>/, "");

When the ? character immediately follows any of the other quantifiers (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy. A non-greedy pattern matches as little of the searched string as possible, whereas the default greedy pattern matches as much of the searched string as possible.


A child of five would understand this. Send someone to fetch a child of five.
Groucho Marx

NanakiXIII
08-22-2008, 11:35 AM
I see, I wasn't aware that the question mark did that as well. Thanks for the explanation.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum