开发者

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

开发者 https://www.devze.com 2023-01-03 10:42 出处:网络
how to split the numeric 开发者_运维技巧values from alphanumeric string value using java script?

how to split the numeric 开发者_运维技巧values from alphanumeric string value using java script?

for example,

x = "example123";

from this i need 123

thanks.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消