93

Suppose I have a string like - "you can enter maximum 500 choices". I need to extract 500 from the string.

The main problem is the String may vary like "you can enter maximum 12500 choices". So how to get the integer part?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Devi
  • 931
  • 1
  • 6
  • 3
  • 2
    You should click the check mark by the answer given by cletus. It was more than sufficient. – Chance Feb 12 '15 at 21:17

9 Answers9

153

Use a regular expression.

const r = /\d+/;
const s = "you can enter maximum 500 choices";
alert (s.match(r));

The expression \d+ means "one or more digits". Regular expressions by default are greedy meaning they'll grab as much as they can. Also, this:

const r = /\d+/;

is equivalent to:

const r = new RegExp("\\d+");

See the details for the RegExp object.

The above will grab the first group of digits. You can loop through and find all matches too:

const r = /\d+/g;
const s = "you can enter 333 maximum 500 choices";
const m;
while ((m = r.exec(s)) != null) {
  alert(m[0]);
}

The g (global) flag is key for this loop to work.

Sammeeey
  • 69
  • 6
cletus
  • 616,129
  • 168
  • 910
  • 942
  • 1
    Minor typo with the backslash in the string: var r = new RegExp("\d+"); should be var r = new RegExp("\\d+"); – Bambam Nov 23 '22 at 08:53
  • the 1st regular expression works. The second one (`RexExp(...)`) doesn't. Thank you @bambam Just saw your comment after I figured it out on my own. – Sammeeey Jul 09 '23 at 09:07
38

var regex = /\d+/g;
var string = "you can enter maximum 500 choices";
var matches = string.match(regex);  // creates array from matches

document.write(matches);


References:

‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ regular-expressions.info/javascript.html (archive)

‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp (archive)

jesterjunk
  • 2,342
  • 22
  • 18
18

const str = "you can enter maximum 500 choices";
const result = str.replace(/[^0-9]/g, "");
console.log(result); // "500"

playground: https://regex101.com/r/1ADa3c/1

Artem Belik
  • 472
  • 4
  • 7
11

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
Dudi
  • 3,069
  • 1
  • 27
  • 23
  • 1
    Did try your regex? It matches any `e` character (or any other characters you have in that list, "E", "," or "."). For your input `matches` is `[ 'e', 'e', '5,123.6', 'e' ]` – Boris Verkhovskiy Nov 20 '19 at 16:55
6

You can also try this :

var string = "border-radius:10px 20px 30px 40px";
var numbers = string.match(/\d+/g).map(Number);
console.log(numbers)
6

I thought I'd add my take on this since I'm only interested in the first integer I boiled it down to this:

let errorStringWithNumbers = "error: 404 (not found)";        
let errorNumber = parseInt(errorStringWithNumbers.toString().match(/\d+/g)[0]);

.toString() is added only if you get the "string" from an fetch error. If not, then you can remove it from the line.

Jimmy Westberg
  • 205
  • 4
  • 10
5

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);
arvind
  • 51
  • 1
  • 1
1
// 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:

https://www.w3schools.com/jsref/jsref_obj_regexp.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

https://www.tutorialspoint.com/javascript/javascript_regexp_object.htm

0

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"));
Kirilo Lozitsky
  • 153
  • 2
  • 7