$string =~ s/(?)($swapString)(?!<\/B>)/$1<\/B>/gi; # this gives: there is a foo bar and a bar foo and also foo and bar.