The easiest way:
var str = "example123";
str = str.replace(/[^0-9]+/ig,"");
alert(str);
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/>");
}
source
share