in reply to How to use \W in Regular Expression?

\W means Match non-word character. As almut said its not required for replace what you are trying to do. Just s/$query/<em>$query</em>/i would be fine. Perl regex