How to get integer 40 from the text "his age is 40"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to get integer 40 from the text "his age is 40"?

I tried the parseInt() method but it is returning NaN. Is there anyother way to get that integer?

9th May 2020, 9:42 PM
Hassan Raza
Hassan Raza - avatar
1 Answer
+ 5
Using regex. let text = "His age is 40"; let age = parseInt(text.match(/\d+/g, text)); console.log(age); //40
9th May 2020, 9:51 PM
Russ
Russ - avatar