开发者

Given a string, how do I strip out all the symbols and keep only alphabets and numbers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.

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 question
Hello$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);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜