2017-04-18 85 views
0

寻找一个正则表达式允许低于2个邮政编码阿鲁巴和巴巴多斯JavaScript的正则表达式支持阿鲁巴和巴巴多斯邮政编码

- Aruba - 0000AW (Only 6 characters are allowed in the format 4 
    digit(only 0) at the beginning and 2 characters (only A and W) at the 
    end) 

- Barbados - BB15094 (Only 7 characters are allowed in the format 2 
    characters(only B) at the beginning and 4 digits (0 t0 9) at the end) 

有人可以帮我拿到这个正则表达式?

我想巴巴多斯 - /^[Bb]{2}?[0-9]{5}$/和阿鲁巴 - /^[0]{4}?[Aa]{1}[Ww]{1}$/

+2

30分钟[花在这里](https://regexone.com/)将教你如何写下它。 –

+0

@WiktorStribiżew感谢thids链接。 对于巴巴多斯我试过/^[Bb] [Bb]?[0-9] [0-9] [0-9] [0-9] [0-9] $ /,它的工作原理。但是我不想重复[0-9] 5次。能否请你帮忙 ? – Kenta

+0

您错过了[*第6课:抓住一些zzz的*](https://regexone.com/lesson/repeating_characters)。 –

回答

0

这里是你的正则表达式的 “清洁剂” 的版本:

巴巴多斯(demo):

/^b{2}\d{5}$/i 

Aruba

/^0{4}aw$/i 

注意区分大小写的修饰符/i允许减少[aA]a等 你不需要附上single chars into character classes,[0] = 0。使用具有确切出现次数的延迟限制量词是没有意义的,{4}? = {4}{1}量词默认为冗余的,因为默认情况下所有原子匹配单个出现(\d[a-z]匹配一个数字后跟一个小写ASCII字母)。

请注意,在JS正则表达式中,\d等于[0-9]