问题
I have the below code. It works only when I have 2 digits. If I have 1 digit doesn't work. I want to work in both cases: one or two digit.var numberRegex = /^[1-9][0-9]$/;
I've tried something like this but unfortunately doesn't work:var numberRegex = /^[1-9]?[1-9][0-9]$/;
Thanks for support.
回答1:
Try this one out:
/^\d{1,2}$/;
Reading what you have it looks like you don't want to accept numbers like 01
.
/^\d{1}|[1-9]\d{1}$/;
回答2:
Try this.
/^[0-9]|[0-9][0-9]$/
This should do the job. Using an Or operator does it.
回答3:
try this regex: /^[1-9]\d{0,1}$/
回答4:
This works:
/^([0-9]{0,1}([1-9][0-9]){0,2})$/
来源:https://stackoverflow.com/questions/10771269/one-or-two-numeric-digits-regex