[英]Regex for 0 or x - y characters
I'm trying to write up a regex for a validation JavaScript, that allows the user to input 4-9 numbers (as the string length), but the user should also be able to leave the field blank. 我正在尝试为验证JavaScript编写一个正则表达式,允许用户输入4-9个数字(作为字符串长度),但用户也应该能够将该字段留空。
The 4-9 numbers part is easy: ^[0-9]{4,9}$
, but I'm struggling on the "not required" part. 4-9数字部分很简单:
^[0-9]{4,9}$
,但我在“不需要”部分挣扎。
I've tried ^[0-9]{4,9}|[0-9]{0}$
but that doesn't work at all. 我已经尝试了
^[0-9]{4,9}|[0-9]{0}$
但这根本不起作用。
只需使您现有的模式可选:
^([0-9]{4,9})?$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.