Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g
flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000
or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With