使用正则表达式过滤固定长度为0或4位的年份 [英] Using regex to filter year of fixed length 0 or 4 digit
本文介绍了使用正则表达式过滤固定长度为0或4位的年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在php或javascript中使用正则表达式过滤年份。
i want to filter year using regex in php or javascript.
仅包含数字且长度为0(如果没有输入)或4如果(插入)
它不接受前123的1,2或3长度。
which contain only number and its length is 0(if not input) or 4 if(insert) it is not accept 1, 2 or 3 length for ex 123.
我知道正则表达式为0到4位数,这是 ^ [0-9] {0,4} $
或 ^ \d {0,4} $
i know regex for 0 to 4 digit which is ^[0-9]{0,4}$
or ^\d{0,4}$
推荐答案
请尝试以下方法:
^([0-9]{4})?$
^
- 行开头
([0-9] {4})?
- 四个数字,可选(因为?
)
([0-9]{4})?
- four numbers, optionally (because of the ?
)
$
- 结束line
$
- end of line
这篇关于使用正则表达式过滤固定长度为0或4位的年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文