are you sure about that? at 1st sight seems that you're using lookbehind assertion inccorectly... so i run a test code over you function:
#!/usr/bin/perl -w use strict; my $terms = 'tree crow black blue green stars'; my $color = '#CC0000'; my $string1= qq!\tthe black crow is sitting in the <font color="blue"> +tree</font> looking at the green stars!; my $string2= qq!\tthe black crow is sitting in the blue tree looking a +t the green stars!; print 'Terms: '.$terms.$/x2; print 'text1:'.$/.$string1.$/; print 'parsed1:'.$/.HTMLhighlight($terms,$string1,$color).$/; print 'text2:'.$/.$string2.$/; print 'parsed2:'.$/.HTMLhighlight($terms,$string2,$color).$/; sub HTMLhighlight { my($searchterms,$instr,$color) = @_; my (@terms)=split(/\ /,$searchterms); foreach my $i (0..$#terms) { $instr =~ s/(?!<.*)($terms[$i])(?!.*>)/<b style=\"background:$colo +r\">$1<\/b>/gi; } return $instr; } # a easier way to do the same thing =] sub HTMLhighlight2 { my ($searchterms,$instr,$color) = @_; $searchterms =~ s/\s+/|/g; $instr =~ s#($searchterms)#<b style="background:$color">$1</b>#gi; return $instr; }

the output is something like this (hope that *code* will keep html tags parsing off :)) ):

Terms: tree crow black blue green stars text1: the black crow is sitting in the <font color="blue">tree</font> lo +oking at the green stars parsed1: the black crow is sitting in the <font color="blue">tree</font> lo +oking at the <b style="background:#CC0000">green</b> <b style="backgr +ound:#CC0000">stars</b> text2: the black crow is sitting in the blue tree looking at the green st +ars parsed2: the black crow is sitting in the blue <b style="background:#CC0000 +">tree</b> looking at the <b style="background:#CC0000">green</b> <b +style="background:#CC0000">stars</b>

not so good.. i don't have yet an elegant solution for exempting searchterms to be parsed when inside html code but still provided you the htmlhighlight2 .. that look a little bit more efficient ;-] (ofc, skipped the futile lookbehind assertions)

--
AltBlue.


In reply to RE: HTMLHighlight by AltBlue
in thread HTMLHighlight by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.