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!

2
  • @Cerbrus question you linked is totally different from this. So I'm going to reopen this.. Commented Jul 15, 2015 at 14:10
  • Hm, looks like I didn't read the other question properly. My bad. Commented Jul 15, 2015 at 14:11

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

And maybe you know whats the regex for ONLY one number?
1
   if (testString.match(/(\d+)/).length >= 2) {

Is a very simple/readable solution.

Comments

0
"String0With1Some2Words3And4Integers0000".split('').reduce((val, acc) => (
    val  + (parseInt(acc) ? 1 : 0))
, 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.