Enter the negative character class ( \W ) and include the hyphen:
MyString = new RegExp('(^|[^a-z0-9_-])' + xy + '([^a-z0-9_-]|$)', 'i');
While \W matches any character without a word (everything except letters, numbers and underscores), [^a-z0-9_-] matches any character that is neither a word character nor a hyphen.
Of course, you can use \W in this character class:
MyString = new RegExp('(^|[^\\w-])' + xy + '([^\\w-]|$)', 'i');
source share