Remove the \s
from your new regex and it should work - whitespace is already included in "anything but alphanumerics".
Note that you may want to add a +
after the ]
so you don't get sequences of more than one underscore. You can also chain onto .replace(/^_+|_+$/g,'')
to trim off underscores at the start or end of the string.