0

I'm having a difficult time creating regex and javascript code to count occurrences of this pattern:

<tr><td align=right>*</td><td align=right>*</td><td><a href="*">*</a></td></tr>

where as * is wild.

How can I go about searching for that?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
willium
  • 2,048
  • 5
  • 25
  • 34
  • 1
    no, the entire string, where as each * is a wildcard (can be anything) – willium Jan 25 '12 at 05:34
  • 3
    Putting regex aside for a moment, what are you actually trying to achieve? (Regex may not be the best way: certainly [it's not very good at parsing html](http://stackoverflow.com/a/1732454/615754).) Are you saying that you have html markup already in a string? – nnnnnn Jan 25 '12 at 05:49

4 Answers4

1

If all you want to do is count occurences of that entire pattern, you can try:

// basically, numMatches = myString.match(/regexhere/ig).length;
numMatches = mystring.match(/<tr><td align=right>[\s\S]*?<\/td><td align=right>[\s\S]*?<\/td><td><a href="[^"]+">[\s\S]*?</a><\/td><\/tr>/ig).length;

The regex is the string you have above, except:

  • * turns into [\s\S]*?. Usually, I'd replace with .*? but in regex . matches everything except newline characters. So, if your string spans multiple lines, it won't match. The usual way to get around this is to use the "dotall" regex modifier that allows . to match newlines too, but javascript doesn't support this (?!). [\s\S] is an alternative in this case.
  • escaped occurences of / in the regex as I'm using / as my regex delimiter for javascript.

Note -- if you want to match where the tags may be split over multiple lines, e.g.

<tr>
  <td align=right>randomstuffhere</td>
  <td><a href="http://somelink">someotherstuff</a></td>
</tr>

Then change the regex so there's a \s* between every tag, which will allow whitespace in between. i.e.:

numMatches = mystring.match(/<tr>\s*<td align=right>[\s\S]*?<\/td>\s*<td align=right>[\s\S]*?<\/td>\s*<td>\s*<a href="[^"]+">[\s\S]*?</a>\s*<\/td>\s*<\/tr>/ig).length;
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
1

Fun with jQuery:

var $matches = $('td[align=right] ~ td[align=right] ~ td > a')

$matches.length // the number of matches
$matches.attr('href') // the value of the anchor's `href` attribute
$matches.parent().prev().text() // the preceding cell's text
$matches.parent().prev().prev().text()
davidchambers
  • 23,918
  • 16
  • 76
  • 105
0

You can build on this, which will match on any tag contents:

(?:.*?\>([^\<\>]+)\<)

And this for matching the href:

href\s*=\s*(["']).*?\1
Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
0

jQuery way (may not be efficient),

var count = 0;
$('td[align=right],a[href=*]').each(function(){
if($(this).html() == '*'){
count++;
}
})

console.log(count);
Jashwant
  • 28,410
  • 16
  • 70
  • 105