RegEx for matching UK Postcodes

前端 未结 30 3273
广开言路
广开言路 2020-11-22 01:38

I\'m after a regex that will validate a full complex UK postcode only within an input string. All of the uncommon postcode forms must be covered as well as the usual. For in

30条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 01:55

    Here's a regex based on the format specified in the documents which are linked to marcj's answer:

    /^[A-Z]{1,2}[0-9][0-9A-Z]? ?[0-9][A-Z]{2}$/
    

    The only difference between that and the specs is that the last 2 characters cannot be in [CIKMOV] according to the specs.

    Edit: Here's another version which does test for the trailing character limitations.

    /^[A-Z]{1,2}[0-9][0-9A-Z]? ?[0-9][A-BD-HJLNP-UW-Z]{2}$/
    

提交回复
热议问题