How to separate letters and digits from a string in JavaScript - Stack Overflow

I have a string which is bination of letters and digits. For my application I have to separate a string

I have a string which is bination of letters and digits. For my application I have to separate a string with letters and digits: ex:If my string is "12jan" i hav to get "12" "jan" seperately..

I have a string which is bination of letters and digits. For my application I have to separate a string with letters and digits: ex:If my string is "12jan" i hav to get "12" "jan" seperately..

Share Improve this question edited Nov 30, 2010 at 7:10 karim79 343k67 gold badges419 silver badges407 bronze badges asked Nov 30, 2010 at 7:03 sandeepsandeep 2,8929 gold badges47 silver badges56 bronze badges 1
  • 4 didnt you just ask this question? look for the answers there you can use the same regular expressions with some minor modification and it will work with javascript aswell stackoverflow./questions/4311156/… – Breezer Commented Nov 30, 2010 at 7:05
Add a ment  | 

2 Answers 2

Reset to default 7

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744113851a4559080.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信