I have a string like this:
var str = "I'm a very^ we!rd* Str!ng.";
What I would like to do is removing all special characters from the above string and replace spaces and in case they are being typed, underscores, with a – character.
The above string would look like this after the “transformation”:
var str = 'im-a-very-werd-strng';
replace(/[^a-z0-9\s]/gi, '')will filter the string down to just alphanumeric values andreplace(/[_\s]/g, '-')will replace underscores and spaces with hyphens:Source for Regex: RegEx for Javascript to allow only alphanumeric
Here is a demo: http://jsfiddle.net/vNfrk/