var regex = /\d+/g;
var string = "you can enter maximum 500 choices";
var matches = string.match(regex); // creates array from matches
document.write(matches);
I like @jesterjunk answer,
however, a number is not always just digits. Consider those valid numbers:
"123.5, 123,567.789, 12233234+E12"
So I just updated the regular expression:
var regex = /[\d|,|.|e|E|\+]+/g;
var string = "you can enter maximum 5,123.6 choices";
var matches = string.match(regex); // creates array from matches
document.write(matches); //5,123.6
var regex = /\d+/g;
var string = "you can enter 30%-20% maximum 500 choices";
var matches = string.match(regex); // creates array from matches
document.write(matches);
// stringValue can be anything in which present any number
`const stringValue = 'last_15_days';
// /\d+/g is regex which is used for matching number in string
// match helps to find result according to regex from string and return match value
const result = stringValue.match(/\d+/g);
console.log(result);`
output will be 15
If You want to learn more about regex here are some links:
Now this is very easy to do using the 'replace' method and 'regexp'. For example:
findNumber = str => {
return +(str.replace(/\D+/g, ''));
}
console.log(findNumber("you can enter maximum 500 choices"));
console.log(findNumber("you can enter maximum 12500 choices"));