0

I'm trying to match empty strings, non-digit characters or numbers with more than 1 digit.

Examples:

"", "a", "abc", "10"

I tried:

/^([^1-9]*)|(\d{2,})$/

but it doesn't work.

2
  • It works for me!? Commented Jan 26, 2012 at 20:12
  • @JosephSilber For some reason it also matches single digit numbers. Commented Jan 26, 2012 at 20:25

1 Answer 1

2

You can use following regex for that:

/^(\d{2,}|[^\d]+|)$/
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I still have some doubts though: /^(\d{2,}|[^\d]+|)$/ works, /^([^\d]*)|(\d{2,})$/ doesn't work, but /^(([^\d]*)|(\d{2,}))$/ works? Why?
You do need to group all or conditions in a bracket that's why /^[^\d]*|\d{2,}$/ didn't work for you but /^([^\d]*|\d{2,})$/ did.
But how can this be called? If I do example.match(regex) it breaks because example is empty.
@mrfox This is built for allowing empty strings also. May I know what is the value of the your example string?
@anubhava the value of my example is an input field from the user, if the field is left empty it will be null. But this has been solved: check for null beforehand and set example to "".

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.