Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I'm trying with
function hasANumber(value) { return /^.*[0-9]*.*$/.test(value); }
Where I was wrong?
/[0-9]+/
* 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:
Add a comment
Just \d would be enough for this case.
\d
> /\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.
[0-9]*
This is the correct form to write the number regex is :
\d+$
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
/[0-9]+/