角NG-模式正则表达式code为美国邮编codeS和加拿大邮政codeS美国、和加、邮编、邮政

2023-09-13 03:40:32 作者:亿人不及你一人

我能有人帮我吗?

我要寻找一些正则表达式使用来验证美国邮编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)$/"
 
精彩推荐
图片推荐