开发者

How to separate letters and digits from a string in JavaScript

I have a string which is combination of letters and digits. For my application I have to separate a string with letters and digits: ex:If 开发者_如何学Pythonmy string is "12jan" i hav to get "12" "jan" seperately..


You can always do this if str is your string:

var digits = str.replace(/\D/g, ""),
    letters = str.replace(/[^a-z]/gi, "");   

Essentially, what this code does is replace all of the characters that you do not want with the empty string.

\D and [^a-z] are character classes that represent, respectively, all the non-digits and all the non-letters. The g at the end of the two expressions makes them replace all occurrences of the pattern. The i make it case-insensitive, keeping both lower and upper case letters.


Well for the example you gave I would try

parseInt(num)

so if you had parseInt(12Jan); you should get 12.

I am not an expert so I hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜