views:

42

answers:

1

how to split the numeric values from alphanumeric string value using java script?

for example,

x = "example123";

from this i need 123

thanks.

+3  A: 

Easiest way would be:

var str = "example123";
str = str.replace(/[^0-9]+/ig,"");
alert(str); //Outputs '123'

However, for string "example123example123" - it will return "123123". If you need to get both numbers as separate values, then it would be a little more complex:

var str="Hello 123 world 321! 111";
var patt=/[0-9]+/g;

while (true) {
    var result=patt.exec(str);
    if (result == null) break;
    document.write("Returned value: " + result+"<br/>");   
}
//Outputs:
//Returned value: 123
//Returned value: 321
//Returned value: 111
Max