Is there a regex pattern to capture numbers in a string up to the first alpha character?

I have a need to capture numbers in a string up to the first alpha character, for example if I have the string:

"120MG 8 times a week"

I want to capture only the "120" and disregard the rest of the string.

>Solution :

Does it have to be regex?
If regex is not required, normal parseInt will get you the same result

console.log(parseInt("120MB 8"))

Leave a Reply