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
  • 166,785
  • 24
  • 204
  • 249
0
   if (testString.match(/(\d+)/).length >= 2) {

Is a very simple/readable solution.

Jason Cust
  • 10,263
  • 2
  • 31
  • 42
endy
  • 3,852
  • 5
  • 27
  • 43
0
"String0With1Some2Words3And4Integers0000".split('').reduce((val, acc) => (
    val  + (parseInt(acc) ? 1 : 0))
, 0)
morten.c
  • 3,346
  • 5
  • 38
  • 44
Ilya Ilin
  • 2,043
  • 19
  • 27