Reputation: 17053
Why doesn't this assign prepClass to the string selectorClass with underscores instead of non alpha chars? What do I need to change it to?
var regex = new RegExp("/W/", "g");
var prepClass = selectorClass.replace(regex, "_");
Upvotes: 1
Views: 238
Reputation: 827324
A couple of things:
RegExp
constructor, you don't need the slashes, you are maybe confusing it with the syntax of RegExp
literals.\W
character class.The following will work:
var regex = new RegExp("\\W", "g");
The RegExp
constructor accepts a string containing the pattern, note that you should double escape the slash, in order to get a single slash and a W ("\W"
) in the string.
Or you could simply use the literal notation:
var regex = /\W/g;
Recommended read:
Upvotes: 4