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?
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?
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./
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;
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()
You can build on this, which will match on any tag contents:
(?:.*?\>([^\<\>]+)\<)
And this for matching the href:
href\s*=\s*(["']).*?\1
jQuery way (may not be efficient),
var count = 0;
$('td[align=right],a[href=*]').each(function(){
if($(this).html() == '*'){
count++;
}
})
console.log(count);