How to get a number value of a string in Javascript?



To get an integer in the string β€œabcde 30”, use the following regex in JavaScript, else it will return NaN. With that, use parseInt() to get the numbers with regex under String match() method βˆ’

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var myStr = "abcdef 30";
         var num = parseInt(myStr.match(/\d+/))
         alert(num);
      </script>
   </body>
</html>
Updated on: 2020-06-17T06:24:14+05:30

530 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements