Javascript 英国邮政编码正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11081786/
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 UK postcode regex
提问by Phil Young
I have a javascript regex which validates UK postcodes. It works well, but it doesn't take into account that some people write it with spaces in the middle and others don't. I've tried to add this but cant work it out :S UK postcodes are mainly 2 letters followed by 1 or 2 numbers, optional whitespace & 1 number and 2 letters.
我有一个验证英国邮政编码的 javascript 正则表达式。它运行良好,但它没有考虑到有些人在中间有空格而其他人则没有。我试图添加这个但无法解决:S 英国邮政编码主要是 2 个字母,后跟 1 或 2 个数字,可选空格和 1 个数字和 2 个字母。
Here is my regex which validates postcodes without spaces:
这是我的正则表达式,它验证没有空格的邮政编码:
[A-PR-UWYZa-pr-uwyz0-9][A-HK-Ya-hk-y0-9][AEHMNPRTVXYaehmnprtvxy0-9]?[ABEHMNPRVWXYabehmnprvwxy0-9]?{1,2}[0-9][ABD-HJLN-UW-Zabd-hjln-uw-z]{2}|(GIRgir){3} 0(Aa){2})$/g
Any ideas?
有任何想法吗?
Edit
编辑
I changed the regex as I realised one group was missing lowercase chars.
当我意识到一组缺少小写字符时,我更改了正则表达式。
回答by Qtax
2 letters followed by 1 or 2 numbers, optional whitespace & 1 number and 2 letters.
2 个字母后跟 1 个或 2 个数字,可选空格和 1 个数字和 2 个字母。
Example:
例子:
/^[a-z]{2}\d{1,2}\s*\d[a-z]{2}$/i
Explained with http://www.myregextester.com/
用http://www.myregextester.com/解释
^ the beginning of the string
----------------------------------------------------------------------
[a-z]{2} any character of: 'a' to 'z' (2 times)
----------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
----------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
[a-z]{2} any character of: 'a' to 'z' (2 times)
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
回答by Mathias Bynens
An alternative solution would be to remove all spaces from the string, then run it through the regular expression that you already have:
另一种解决方案是从字符串中删除所有空格,然后通过您已经拥有的正则表达式运行它:
var postalCode = '…';
postalCode = postalCode.replace(/\s/g, ''); // remove all whitespace
yourRegex.test(postalCode); // `true` or `false`
回答by Phil H
According to an archived page I found, the spec is:
根据我发现的存档页面,规范是:
Pattern w/o space RLE General
AN NAA ANNAA A1 N2 A2 | A{1,2} N{2,3} A2
ANN NAA ANNNAA A1 N3 A2 |
AAN NAA AANNAA A2 N2 A2 |
AANN NAA AANNNAA A2 N3 A2 |
ANA NAA ANANAA A1 N1 A1 N1 A2 | A{1,2} N1 A1 N1 A2
AANA NAA AANANAA A2 N1 A1 N1 A2 |
GIR 0AA We are British and for every rule there must be an
equal and opposite exception.
Maybe this is too much hassle to bother with in a page validation. Remember, you'll have to maintain it if there's ever a change. Consider a minimum check like ^[A-Z].+[0-9].+[A-Z]$
. Don't be a 'hero' and boilerplate the code.
也许这在页面验证中太麻烦了。请记住,如果有任何变化,您必须维护它。考虑像^[A-Z].+[0-9].+[A-Z]$
. 不要成为“英雄”,而是将代码样板化。
If you really want to validate it against that spec, the general rules (after stripping whitespace) are:
如果您真的想根据该规范对其进行验证,则一般规则(在去除空格后)是:
^([A-Z]{1,2})([0-9]{2,3})([A-Z]{2})$/i
^([A-Z]{1,2})([0-9])([A-Z])([0-9])([A-Z]{2})$/i
^GIR0AA$/i
As @Stefan pointed out: /i for case-insensitivity.
正如@Stefan 指出的那样: /i 不区分大小写。
Once you have done this, you can match the groups (hence the braces), and check that the letters match the restricted ranges in the document. At this point you can even maintain a list of allowed one and two-letter codes for postcode areas.
完成此操作后,您可以匹配组(因此是大括号),并检查字母是否与文档中的限制范围匹配。此时,您甚至可以为邮政编码区域维护一个允许的单字母和双字母代码列表。
The general rule for separating the Incode (chunk before the space) from the Outcode (chunk after the space) seems to be that the Outcode starts from the last number (even for GIR).
将 Incode(空格前的块)与 Outcode(空格后的块)分开的一般规则似乎是 Outcode 从最后一个数字开始(即使对于 GIR)。
Frankly, I would stop bothering after the basic check. If it's worth validating against a more complete spec, then it's probably worth checking that the postcode area exists, and if that's worthwhile you might as well connect to a real service that extracts the address from the postcode. Those services will happily inform you that a postcode doesn't exist, which is a more robust and maintainable check than you could ever want to write.
坦率地说,我会在基本检查后不再打扰。如果值得根据更完整的规范进行验证,那么可能值得检查邮政编码区域是否存在,如果值得,您不妨连接到从邮政编码中提取地址的真实服务。这些服务会很高兴地通知您邮政编码不存在,这是一种比您想编写的更健壮和可维护的检查。
[Edit: there's another spec on Wikipediaof course]
[编辑:当然维基百科上还有另一个规范]
回答by designgroop
We've found that most regex don't fully cover UK post codes. We found it needs to cover these options:
我们发现大多数正则表达式并未完全涵盖英国邮政编码。我们发现它需要涵盖以下选项:
- A1 1AA
- A1A 1AA
- AA11 1AA
- A1 1AA
- A1A 1AA
- AA11 1AA
Based on that, I'd recommend:
基于此,我建议:
/^([A-Z][A-Z0-9]?[A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z0-9]{2})$/;
/^([A-Z][A-Z0-9]?[A-Z0-9]?[A-Z0-9]? {1,2}[0-9][A-Z0-9]{2})$/;
The GIR0AA
postcode used to belong to Girobank but is no longer supported, see reference:
http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
该GIR0AA
邮政编码曾经属于通汇转帐制,但不再支持,请参考:
http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
回答by Zagorax
I don't know how UK postal code are done, and I didn't understand it from your description. By the way, you can add \s{0,1}
where you need to say "here we could have a space". It means "0 or 1 space".
不知道英国的邮政编码是怎么做的,看你的描述没看懂。顺便说一句,您可以\s{0,1}
在需要说“这里有一个空间”的地方添加。它的意思是“0 或 1 个空格”。