1

I have this string :

testString = "child 4 to 10 years old";

How to check if the string contains two (or more) numbers?

testString.match("??")

Thanks!

3 Answers3

3

This would find a match if the string contains atleast two numbers.

testString.match(/(?:.*?\b\d+\b){2}/)

or

/(?:.*?\b\d+\b){2}/.test(str);

or

If you also want to deal with decimal numbers then try this,

/(?:.*?\b\d+(?:\.\d+)?\b){2}/.test(str);
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1
   if (testString.match(/(\d+)/).length >= 2) {

Is a very simple/readable solution.

Jason Cust
  • 10,743
  • 2
  • 33
  • 45
endy
  • 3,872
  • 5
  • 29
  • 43
0
"String0With1Some2Words3And4Integers0000".split('').reduce((val, acc) => (
    val  + (parseInt(acc) ? 1 : 0))
, 0)
morten.c
  • 3,414
  • 5
  • 40
  • 45
Ilya Ilin
  • 2,283
  • 21
  • 27