in reply to Re^2: Highlight a substring
in thread Highlight a substring
By the way,
$string1 =~ s/($string2)/<b>$1<\/b>/g;
should be replaced with
$string1 =~ s/(\Q$string2\E)/<b>$1<\/b>/g;
in case $string2 contains special characters. Better yet,
$string1 =~ s/((?:\Q$string2\E)+)/<b>$1<\/b>/g;
should be a little bit faster.
I'd also Benchmark the following which has fewer concatenations:
$string1 =~ s#(?=(?:\Q$string2\E)+)#<b>#g; $string1 =~ s#(?<=(?:\Q$string2\E)+)#</b>#g;
|
|---|