in reply to RexExp help: Highlight keywords in CGI search results, unless inside an HTML tag

I always think that parsing HTML with regexp's is bad and go for something like HTML::Parser or HTML::TreeBuilder everytime. This may or may not be an overkill for your situation.
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^

  • Comment on Re: RexExp help: Highlight keywords in CGI search results, unless inside an HTML tag
  • Download Code