>>584
理解できなかった理由は「長すぎたから」では無かろ。

#!/usr/bin/perl -w
use strict; use CGI qw(escapeHTML);
my $str = <<'EOH';
あああ<span title="FONT">FONT</span>www
<span title="www
www">あああaaaFONTwww</span
>aaa<span>FONT<span title="aaaあああaaa">wwwあああwww
</span>FONT</span>
EOH
print 'あああ = ', mark(\$str => 'あああ', with => '#00ff00'), "\n";
print mark($str => 'FONT', with => '#0000ff');

sub mark {
  my($str, $target, %css) = @_;
  With: {
    defined $css{'with'} or last With;
    local $_ = delete $css{'with'};
    /^#/ and $css{'background-color'} = $_, last With;
  }
  $css{'font-weight'} ||= 'bold';
  my $css = escapeHTML(join ' ', map "$_:$css{$_};", keys %css);

  # http://www.din.or.jp/~ohzaki/regex.htm#ReplaceOutside
  my $count = (ref $str ? ${$str} : $str) =~
    s{ ((?:\G|>)[^<]*?) (\Q$target\E) } {$1<span style="$css">$2</span>}gx;

  ref $str ? $count : $str;
}