Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading