我能有人帮我吗?
我要寻找一些正则表达式使用来验证美国邮编codeS和加拿大邮政codeS在AngularJS
I am looking for some regexp to use to validate US Zip codes and Canadian Postal codes in AngularJS
这应该为你工作:
^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$
http://regex101.com/r/nO4zF5
删除 ^
和 $
锚,如果你不想字符串的开始和结束匹配
Remove the ^
and $
anchors if you don't want to match the start and end of the string.
当使用在Javascript记住,你需要使用 /
分隔符:
When using in Javascript remember that you need to use the /
delimiter:
if (/^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$/.test(str)) {
// it's a match!
}
和直接,作为一个角 NG-模式
:
ng-pattern="/^(\d{5}(-\d{4})?|[A-Z]\d[A-Z] *\d[A-Z]\d)$/"