$string =~ s/([^]($swapString)[^<\/B>])/$1<\/B>/gi; there is a foo bar and a bar foo and also foo and bar.