<input class="input-width" placeholder="XXXXX" maxlength="10"
type="text" name="zip" ng-model="shipping.zip" required ng-pattern="/^[0-9]+$/" />
这仅输入数字。我如何输入 5 个数字,然后输入“-”,然后输入 4 个数字。我是正则表达式的新手。谁能帮我创建这个。
最佳答案
这符合您的要求:
^[0-9]{5}(?:-[0-9]{4})?$
即:
characters 0-9
| 5 of them
| |
v v
^[0-9]{5}(?:-[0-9]{4})?$
^^^ ^
| |
| group is optional
|
group (no-capture)
查看注释。在字符串的开头和结尾处还有一个文字 -
破折号以及 ^
和 $
anchor 。
5 位数字,后跟可选的 -
和 4 位数字。
关于javascript - 邮政编码的正则表达式是什么,即 96754-2222,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35277769/