Given a string, how do I strip out all the symbols and keep only alphabets and numbers? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.开发者_运维百科
Improve this questionHello$Barbie^._9
How do i turn that into:
HelloBarbie9
DEMO HERE
var re = /[_\W]/g;
var str = "Hello$Barbie^._9";
str = str.replace(re,"");
\W means anything BUT characters (case INsensitive), numbers AND underscore, so we need to add the underscore
With something like this:
'Hello$Barbie^._9'.replace(/[^A-Za-z0-9]/g, '') // returns "HelloBarbie9"
-- For Comments --
In fact there is a flag for case insensitivity. It's i
. So that would be come:
'Hello$Barbie^._9'.replace(/[^a-z0-9]/ig, '') // returns "HelloBarbie9"
var str = "Hello$Barbie^._9"; str = str.replace(/[^a-zA-Z0-9]/g);
精彩评论