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

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 03-Feb-2018 11:45:16
Advertisements