use HTML::Parser; my $keyword = quotemeta 'match'; sub highlight { my $text = shift; $text =~ s|\b($keyword)\b|$1|g; return $text; } my $html = q{
this is html match thistext html blah
}; my $p = new HTML::Parser(api_version => 3, handlers => { text=> [ sub { print highlight(shift) }, 'text'], default => [sub { print shift }, 'text'], }); $p->parse($html);