Con*_*ddy 17 javascript regex street-address
我想知道是否有最佳的爱尔兰Eircode格式验证方法.我最好的尝试,到目前为止,在JavaScript中使用正则表达式,是基于第11页上找到的官方规格如下这里.
(第11页基于文档中的页码,如果包含封面,则为第12页)
/^[A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{1}[0-9]{1}[0-9,W]{1}[\ \-]?[0-9,A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y]{4}$/
Run Code Online (Sandbox Code Playgroud)
我没有在这里找到任何与Eircode相关的问题,所以我想我会打开这个问题,看看其他人的想法,看看有什么更好/更短/更有效的模式,任何人都可以想出来.
编辑:根据@Asunez回答删除逗号.
/^[ACDEFHKNPRTVWXY]{1}[0-9]{1}[0-9W]{1}[\ \-]?[0-9ACDEFHKNPRTVWXY]{4}$/
Run Code Online (Sandbox Code Playgroud)
Asu*_*nez 14
由于@Manwal的回答并不完全符合它应该做的,这是我试图缩短OP的正则表达式:
(?:^[AC-FHKNPRTV-Y][0-9]{2}|D6W)[ -]?[0-9AC-FHKNPRTV-Y]{4}$
这基本上是你的正则表达式,只有一些变化:
[]括号内的项目.C-F,V-Y)的位置.在其他地方添加范围是不利的,因为它不会使正则表达式更短.也可以D6W专门处理lookbehind,但这更像是一种艺术而不是正则表达.
请参阅正则表达式演示:此处
您也可以将字符类反转为不包含给定字符,虽然它不会使正则表达式更短,但也值得注意.但是,您需要确保不包含其他字符(如点,逗号).我是通过添加\W令牌来实现的.
你可以在这里试试
hyw*_*wak 10
根据产品指南第1.5.4章允许的标志是:
-----------------------------------------------------------------------
| Component | Position | Allowed characters |
-----------------------------------------------------------------------
| Routing Keys | 1 | A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Routing Keys | 2 | 0-9 |
-----------------------------------------------------------------------
| Routing Keys | 3 | 0-9 with the exception of W for D6W |
-----------------------------------------------------------------------
| Unique Identifier | 4 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 5 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 6 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
| Unique Identifier | 7 | 0-9, A,C,D,E,F,H,K,N,P,R,T,V,W,X,Y |
-----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
每个路由密钥必须包含字母和两个数字,除了一个特定情况,即D6W代码.
因此,与begening代码A5W,C6W,V0W是无效的.
根据章节 1.5.1 Recommendations for Storage and Presentation
存储在数据库中的代码不应与space或分开dash,应该分开,但space仅限于显示.
假设,正确的正则表达式应如下所示:
/([AC-FHKNPRTV-Y]\d{2}|D6W)[0-9AC-FHKNPRTV-Y]{4}/
更新了这个答案,避免了 char B。你可以试试这个:
/^[AC-Y]{1}[0-9]{1}[0-9W]{1}[ \-]?[0-9AC-Y]{4}$/
Run Code Online (Sandbox Code Playgroud)
描述:
^ assert position at start of the string
[AC-Y]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
A the literal character A (case sensitive)
C-Y a single character in the range between C and Y (case sensitive)
[0-9]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
0-9 a single character in the range between 0 and 9
[0-9W]{1} match a single character present in the list below
Quantifier: {1} Exactly 1 time (meaningless quantifier)
0-9 a single character in the range between 0 and 9
W the literal character W (case sensitive)
[ \-]? match a single character present in the list below
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
the literal character
\- matches the character - literally
[0-9AC-Y]{4} match a single character present in the list below
Quantifier: {4} Exactly 4 times
0-9 a single character in the range between 0 and 9
A the literal character A (case sensitive)
C-Y a single character in the range between C and Y (case sensitive)
$ assert position at end of the string
Run Code Online (Sandbox Code Playgroud)