Selecting numbers in JavaScript string only
4 answers
The pattern \d+
will only return sequential digits, and since you run exec
once without a parameter g
, it will give you the first occurrence of sequential digits.
Use this:
var re = /\d+/g;
var str = '+12 (345)-678.90[]';
var res = "";
while ((m = re.exec(str)) !== null) {
res += m[0];
}
alert(res);
Conclusion 1234567890
as we add the found digital sequences to the variable res
.
+1
source to share