正则表达式用于数字检查低于值 [英] Regex for number check below a value

查看:40
本文介绍了正则表达式用于数字检查低于值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是正则表达式的初学者,所以目前这有点超出我的水平.

I'm still a beginner at regex so this is a little above me currently.

我需要验证一个输入为两个字符的数字,并且它不能超过值 12.

I need to validate a number input to two characters and it can't be more than the value 12.

这两个数字很简单:

/^\d{1,2}$/

然后检查如果有两个数字,那么第一个必须是 0 或 1,第二个必须是 0、1 或 2.那部分我不知道怎么表达,定期...

Then check that if there are two numbers then the first must be either 0 or 1, and the second must be either 0, 1, or 2. That part I don't know how to express, regularly...

如果可能的话,我希望在单个正则表达式语句中使用它.

And also if possible I would like to have it in single regex statement.

任何帮助表示赞赏.

推荐答案

Regex 不适合处理这样的范围,但无论如何这应该可行:

Regex is not suited to dealing with ranges like this, but anyway this should work:

/^(0?[1-9]|1[012])$/

说明:

^       match start of the string
(0?     optional zero
[1-9]       any number between 1 and 9
|1[012])    or match 1 followed by either a 0, 1, 2 i.e. 10, 11, 12.
$           match end of string

这篇关于正则表达式用于数字检查低于值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆