in reply to RexExp help: Highlight keywords in CGI search results, unless inside an HTML tag
use HTML::Parser; my $keyword = quotemeta 'match'; sub highlight { my $text = shift; $text =~ s|\b($keyword)\b|<span style="background: #FFFFFF">$1</sp +an>|g; return $text; } my $html = q{ <p id="match"><b>this is html match this</b><u value="don't match +this">text html blah</u></p> }; my $p = new HTML::Parser(api_version => 3, handlers => { text=> [ sub { print highlight(shift) }, 'text'], default => [sub { print shift }, 'text'], }); $p->parse($html);
gav^
|
|---|