One or two numeric digits Regex

和自甴很熟 提交于 2019-12-06 18:38:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!