I am creating vanity urls and I want to only allow the users to have
'a-z', 'A-Z', '0-9, '.', ' ', '-', '_' in their urls.
So given a string I would like to strip all characters that are not in this group.
What is the best way to do this?
Originally I was looping over the string and then checking if the character was in an this string.
But that’s obviously not very efficient…
Example: http://jsfiddle.net/6Efc5/1/
This is a regex that matches anything not in the
[^]set globally, and replaces it with an empty string:\walpha-numeric and underscore-dash\sspace.periodEDIT: Because I was in the character set, I really didn’t need to escape the
.and-. Fixed.EDIT2: As noted by @Hailwood, the
\swill match several types of whitespace characters. To allow only a' 'character, and not others like tab or new line characters, replace\swith a simple space.