Javascript Question

Separate characters and numbers from a string

I have a string variable that contain character and numbers like this

var sampleString = "aaa1211"

Note that variable always start with a
and end with
. Character and number size is not fixed. It could be something like followings

var sampleString = "aaaaa12111"
var sampleString = "aaa12111"

I need to separate the characters and numbers and assign them into separate variables.

How could I do that ?

I try to use split and substring but for this scenario I couldn't apply those. I know this is a basic question but i'm search over the internet and I was unable to find an answer.

Thank you

Answer Source

Please use [A-Za-z] - all letters (uppercase and lowercase) [0-9] - all numbers

        function myFunction() {
        var str = "aaaaAZE12121212";
        var patt1 = /[0-9]/g;
        var patt2 = /[a-zA-Z]/g;
        var letters = str.match(patt2);
        var digits = str.match(patt1);
        document.getElementById("alphabets").innerHTML = letters;
     document.getElementById("numbers").innerHTML = digits;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download