I've been using this:
str2 = str1.replace(/[^\w]/gi, '');
It works fine, but falls foul of JSLint for having an insecure '^'
as outlined in the posts here and here.
The consensus is that it is better to use your regex
to specify what is allowed rather than what is not. No one ever demonstrates how to do this, however. I've even got Flanagan and Crockford in front of me here, but to my shame I'm still not sure what to do.
So... how do you set str2
to only allow the \w
characters found in str1
using a positive test rather than a negative one?