HomeRegex LibraryUnited Kingdom Postcode Regex
100% Client-Side β€’ 0 B Data Leaves Browser
validation

United Kingdom Postcode Regex

Validates official UK postcodes across all outward and inward alphanumeric sectors.

Regular Expression Pattern:
/^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/i
Valid Match Example:
SW1A 1AA
Invalid / Non-Match Example:
12345

Regex Syntax Breakdown & Explanation

  • β€’^[A-Z]{1,2}[0-9][A-Z0-9]? : Outward code representing postal area and district
  • β€’ ? : Optional spacing
  • β€’[0-9][A-Z]{2}$ : Inward code pinpointing street unit

Implementation in Popular Languages

JavaScript / TypeScript
const regex = /^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/i;
const isValid = regex.test("SW1A 1AA");
console.log(isValid); // true
Python (re)
import re

pattern = r"^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$"
match = re.match(pattern, "SW1A 1AA")
print(bool(match)) # True
Go (regexp)
package main
import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile(`^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$`)
  fmt.Println(re.MatchString("SW1A 1AA"))
}
Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

The regular expression pattern is /^[A-Z]{1,2}[0-9][A-Z0-9]? ?[0-9][A-Z]{2}$/i. Validates official UK postcodes across all outward and inward alphanumeric sectors.