Skip to content

Instantly share code, notes, and snippets.

@simonwhitaker
Last active December 15, 2023 11:29
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save simonwhitaker/5748487 to your computer and use it in GitHub Desktop.
Save simonwhitaker/5748487 to your computer and use it in GitHub Desktop.
An example of using a simplified UK postcode regex in Javascript
var tweet = "Currently chilling out at W1B 2EL, then on to WC2E 8HA or maybe even L1 8JF! :-)";
// Here's a simple regex that tries to recognise postcode-like strings.
// See http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation
// for the rules on how UK postcodes are formatted.
var postcode_regex = /[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/g;
var postcodes = tweet.match(postcode_regex);
console.log(postcodes);
@simonwhitaker
Copy link
Author

NB: this is a simplified regular expression that just looks for the basic letter/number patterns that are legal in UK postcodes. It won't validate the pattern as a legitimate UK postcode. (e.g. it will match Q1 1QQ, despite the fact that Q is never used as the first letter in a UK postcode.)

For a more comprehensive regular expression, see this Stack Overflow post.

@johnhunt
Copy link

CR0 1DE doesn't seem to match.. this is however a real postcode.

@andymeek
Copy link

The reason why 'CR0 1DE' doesn't work is because the 'i' needs to be added to the end of the regular expression in order to make it case insensitive - for example:

var postcode_regex = /[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/gi;

Hope this helps.

@foxx
Copy link

foxx commented Oct 14, 2014

Seems to match all the correct patterns, thanks!

  A9 9AA
 A9A 9AA
 A99 9AA
 AA9 9AA
AA9A 9AA
AA99 9AA

@diegoarcega
Copy link

it doesnt cover this ones
WC2A
GIR 0AA

https://regex101.com/r/NUiHiD/2

@lucasjahn
Copy link

/^(GIR[ ]?0AA|((AB|AL|B|BA|BB|BD|BH|BL|BN|BR|BS|BT|CA|CB|CF|CH|CM|CO|CR|CT|CV|CW|DA|DD|DE|DG|DH|DL|DN|DT|DY|E|EC|EH|EN|EX|FK|FY|G|GL|GY|GU|HA|HD|HG|HP|HR|HS|HU|HX|IG|IM|IP|IV|JE|KA|KT|KW|KY|L|LA|LD|LE|LL|LN|LS|LU|M|ME|MK|ML|N|NE|NG|NN|NP|NR|NW|OL|OX|PA|PE|PH|PL|PO|PR|RG|RH|RM|S|SA|SE|SG|SK|SL|SM|SN|SO|SP|SR|SS|ST|SW|SY|TA|TD|TF|TN|TQ|TR|TS|TW|UB|W|WA|WC|WD|WF|WN|WR|WS|WV|YO|ZE)(\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}))|BFPO[ ]?\d{1,4})$/

@indirap
Copy link

indirap commented Dec 18, 2017

@MarkBird
Copy link

MarkBird commented Feb 8, 2018

I'd advise anyone arriving here from search engines to avoid the regex in the above comment (it's perhaps typical that the UK government would be using a badly formed regex) because it doesn't match a lot of valid postcodes (e.g. W1T 1PG), and it's inconsistent in that it matches some postcodes with a space and some without. The one posted by lucasjahn seems to work fine though.

@BWLR
Copy link

BWLR commented Aug 20, 2019

Thanks for the heads up @MarkBird
Just arrived here in the future from a search engine.

@tomgreenhill
Copy link

Likewise, thanks @MarkBird!

@Degats
Copy link

Degats commented Apr 21, 2022

As someone else arriving in the future from a search engine, I was curious why the UK Gov's validation didn't work as mentioned by @MarkBird.

Turns out that the regex itself appears to be correct - just not when you copy-paste it. The error is because one of the important '-' characters gets stripped out when you copy it, as it thinks that it's to break a word across a newline.

I'm not sure what the comment about spaces means; looking at the regex, it requires exactly one space (0x20) character, so is perhaps a little pedantic for some uses. It successfuly finds all the example postcodes in @diegoarcega's test (except WC2A, which is not a complete postcode on its own, but is a prefix/district. Full WC2A 0XX postcodes are matched)

This is the probably correct (but pedantic) regex from the UK Gov document linked by @indirap, to save anyone else from the copypaste issue:

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$

Edit: tested and working against the ONS list of ~2.6million real postcodes (all have spaces)

@mtmgoliath
Copy link

Thanks for the helpful post!

My use case needed it to also work with 0 or 1 spaces {0,1}, so here is my slightly modded version for anyone's convenience, including future me!

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment