Sometimes, we want to use regex in JavaScript to return just numbers.
In this article, we’ll look at how to use regex in JavaScript to return just numbers.
How to use regex in JavaScript to return just numbers?
To use regex in JavaScript to return just numbers, we can use the string match method with a regex pattern that match the numbers.
For instance, we write
const numberPattern = /\d+/g;
const numbers = "something102asdfkj1948948".match(numberPattern);
to call match with the numberPattern regex to match all numbers in the string 'something102asdfkj1948948'.
And then we get the results returned as an array.
Conclusion
To use regex in JavaScript to return just numbers, we can use the string match method with a regex pattern that match the numbers.