正则表达式允许空字符串或具有特定长度的字符串(Regular Expression to allow an empty string or a string with specific length)
嗨,我无法尝试创建一个正则表达式,允许我将字段设置为空,或者填充长度为9的字符串。
目前使用的正则表达式是: ^[0-9]{9}
如果该字段是必填字段,则该字段有效,但是我们有一个新要求,即它也可以为空。 我已经尝试了^[0-9]{0|9 },但显然不起作用。
提前致谢
Hi I am having trouble trying to create a regex that will allow me to have the field empty or a string with length 9 if populated.
The current regex im using is: ^[0-9]{9}
Which works if the field is mandatory, however we have a new requirement that this can also be empty. I've tried ^[0-9]{0|9} but obviously that doesnt work.
Thanks in advance
最满意答案
^(|[0-9]{9})$应该做的伎俩。
编辑:但认真; 做@Felix Kling在评论中建议:通过.length属性, .equals("")等测试字符串。
^(|[0-9]{9})$Should do the trick.
Edit: But seriously; do what @Felix Kling suggests in a comment: test the string via a .length property, .equals("") or the like.
更多推荐
发布评论