How to split numeric values ​​from alphanumeric string value using javascript?

how to break numeric values ​​from alphanumeric string value using java script?

eg,

x = "example123";

from this i need 123

thank.

+3
source share
1 answer

The easiest way:

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

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

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
+4
source

Source: https://habr.com/ru/post/1750255/


All Articles