How to get all integer numbers from a string using JavaScript?

Using regular expression:

Finding number from string using regular expression is quite easy. JavaScript match method has option for using regular expression. Following is the code block that will find multiple numbers from string:

var str = 'There are 100 and 1 cases to resolve';
var matches = str.match(/\d+/g).map(Number);
console.log(matches);

Explanation:

 /\d+/g

  • Look for digits one or more globally (find multiple occurrence)

  • The match method returns the number as string but to make it number the map method is used


     

Comments