ISO 3166-1 Alpha-2 Country Code Regex
Validates that a string has the shape of an ISO 3166-1 alpha-2 country code: exactly two uppercase letters, such as US, GB, or IN.
Regex Pattern
^[A-Z]{2}$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| [A-Z] | Matches a single uppercase letter A through Z |
| {2} | Repeats the uppercase letter match exactly twice |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern checks that an input string consists of exactly two uppercase ASCII letters, matching the structural format used by ISO 3166-1 alpha-2 country codes like US, GB, IN, and JP. It does not check the code against the real list of assigned country codes.
Why it works
The character class [A-Z] restricts matches to uppercase Latin letters only, and the quantifier {2} combined with start and end anchors forces the entire string to be exactly two characters long, no more and no fewer, which mirrors the fixed-width structure of the alpha-2 standard.
Common use cases
- Validating a country field in a signup or shipping address form
- Checking the shape of a country parameter before looking it up in a reference table
- Filtering log or CSV data for well-formed country code fields
- Guarding against obviously malformed input before an API call to a geolocation service
Edge cases
- Lowercase input like us is rejected since the pattern requires uppercase letters and no case-insensitive flag is set
- Three-letter codes like USA (ISO 3166-1 alpha-3) are rejected because the length is fixed at exactly two characters
- Codes containing digits, like U1, are rejected since the character class only allows letters
- The pattern accepts any two-letter combination shape-wise, including unassigned codes like XX or ZZ, since it performs no lookup against the real ISO list
Limitations
- Does not validate against the actual set of ~249 assigned ISO 3166-1 codes, only the two-uppercase-letter shape
- Does not handle user-assigned or reserved codes differently from officially assigned ones
- Case must be normalized to uppercase by the caller before validation, since the pattern is case-sensitive by default
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const countryCodeRegex = /^[A-Z]{2}$/;
console.log(countryCodeRegex.test('US')); // trueCommon Mistakes
Adding the case-insensitive flag to accept lowercase codes, which silently accepts malformed casing instead of surfacing normalization bugs upstream
Fix: Normalize input to uppercase explicitly before validation, and keep the pattern case-sensitive
Assuming the regex validates real country codes, and skipping a lookup against the actual ISO 3166-1 list
Fix: Pair this shape check with a lookup table or library for codes that must be guaranteed to be real and currently assigned
Using \w{2} instead of [A-Z]{2}, which also matches digits and underscores
Fix: Use [A-Z]{2} to restrict matches to uppercase letters only, matching the actual alpha-2 alphabet
Performance Notes
- The pattern is fixed-length and anchored, making it effectively O(1) to evaluate regardless of input length beyond the first few characters
- No backtracking is possible since the quantifier is a fixed count with no alternation
- For high-volume validation, a plain length and character-range check can outperform regex, but the regex is clearer and fast enough for most use cases
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |
Work with this pattern
Take this expression further — test it against your own data, adapt it in the builder, or read up on the syntax behind it.
Regex Tester
Test a regular expression against sample text and see every match highlighted live.
Regex Generator
Build a pattern by clicking blocks — no syntax memorisation required.
Regular Expressions Guide
What regex is, how the syntax works, and the mistakes that trip people up.
Regex Cheat Sheet
Every token, quantifier, and assertion on one printable page.
Python Regex Guide
The re module end to end — search, match, findall, groups, and substitution.
Regex Pattern Library
100+ ready-to-use patterns for email, URLs, dates, IPs, and more.