1

I'm trying with

function hasANumber(value) {
    return /^.*[0-9]*.*$/.test(value);
}

Where I was wrong?

1
  • 1
    if you want just to check whether string has at least one digit - /[0-9]+/ Commented Feb 26, 2015 at 0:45

3 Answers 3

1

* in regex means zero or more

You should have used + which means one or more, as in:

/^.*[0-9]+.*$/

Although this can be simplified to:

/[0-9]+/

Sign up to request clarification or add additional context in comments.

Comments

1

Just \d would be enough for this case.

> /\d/.test('foo')
false
> /\d/.test('fo1o')
true

[0-9]* in your regex matches a digit zero or more times, so it would allow also the strings which won't have a digit.

Comments

0

This is the correct form to write the number regex is :

 \d+$

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.