#!/usr/bin/perl -w
use strict;
use HTML::Parser;
my $tagged_string = "textmore text...\nstringanother string";
my $replace = 'string';
my $parser = HTML::Parser->new(api_version => 3,
default_h => [sub {print shift}, 'text'],
text_h => [\&text, 'dtext']);
$parser->parse($tagged_string);
sub text
{
my ( $text ) = @_;
$text =~ s%\b($replace)\b%$1%sg;
print $text;
}