admin管理员组

文章数量:1302930

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

for example,

x = "example123";

from this i need 123

thanks.

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

for example,

x = "example123";

from this i need 123

thanks.

Share Improve this question asked Jun 16, 2010 at 9:31 VinothPHPVinothPHP 8212 gold badges10 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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 plex:

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

本文标签: how to split the numeric values from alphanumeric string value using javascriptStack Overflow