- or download this
$string =~ s/[^<B>]($swapString)[^<\/B>]/<B>$1<\/B>/gi;
- or download this
[^<B>] <- this matches any *single* char that is not a < B >
($swapString) <- this put the match for $swapstring into $1
[^<\/B>] <- this matches any *single* char that is not a < / B >
- or download this
$string =~ s/([^<B>]($swapString)[^<\/B>])/<B>$1<\/B>/gi;
there is a <B>foo bar</B> and a <B>bar foo</B> and also<B> foo </B>and
+<B> bar.</B>
- or download this
$string =~ s/(?<!<B>)($swapString)(?!<\/B>)/<B>$1<\/B>/gi;
# this gives:
there is a <B>foo bar</B> and a <B>bar foo</B> and also <B>foo</B> and
+ <B>bar</B>.