Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

for example,

x = "example123";

from this i need 123

thanks.

like image 858
VinothPHP Avatar asked Jun 16 '10 09:06

VinothPHP


1 Answers

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
like image 197
bezmax Avatar answered Oct 05 '22 11:10

bezmax