4

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 my string is "12jan" i hav to get "12" "jan" seperately..

karim79
  • 339,989
  • 67
  • 413
  • 406
sandeep
  • 2,862
  • 9
  • 44
  • 54
  • 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 http://stackoverflow.com/questions/4311156/how-to-separate-letters-and-digits-from-a-string-in-php/4311176#4311176 – Breezer Nov 30 '10 at 07:05

2 Answers2

6

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.

Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
1

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.

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
Jamie T
  • 33
  • 5