Javascript 加拿大邮政编码的javascript正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12773523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript regex for canadian postal code
提问by DarknessBeginsHere
Possible Duplicate:
Canada postal code validation
可能重复:
加拿大邮政编码验证
I need javascript regex for validating Canadian postal/zip code.
Canada's Postal Code format is 'A1A 1X1' or 'a1a1x1'
. However it doesn't include the letters D, F, I, O, Q, or U.I found few here but those were in C#.
我需要 javascript regex 来验证加拿大邮政编码。加拿大的邮政编码格式为 ' A1A 1X1' or 'a1a1x1'
. 但是,它不包括字母 D、F、I、O、Q 或 UI,这些字母在这里很少见,但它们在 C# 中。
回答by DarknessBeginsHere
function checkPostal(postal) {
var regex = new RegExp(/^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]( )?\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i);
if (regex.test(postal.value))
return true;
else return false;
}
回答by A. Matías Quezada
As the exceptional words have nothing in common, the valid words should be written one by one.
由于例外词没有共同点,有效词应一一书写。
[ABCEGHJKLMNPRSTVWXYZ]
Followed by a digit
后跟一个数字
\d
And this three times
而这三遍
{3}
Finally we add "i" for case-insensitive
最后我们为不区分大小写添加“i”
var regex = /([ABCEGHJKLMNPRSTVWXYZ]\d){3}/i;