ISO 3166-1 alpha-2 Country Code in JS
Validate 2-letter ISO 3166-1 alpha-2 country codes (US, GB, FR, JP, etc.) — structural check only.
Try it in the JS tester →Pattern
regexJS
^[A-Z]{2}$JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^[A-Z]{2}$", "");
const input = "US";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
^[A-Z]{2}$ requires exactly two uppercase letters anchored to the full string. Like the currency-code pattern, this verifies FORMAT not membership — combine with the official list to ensure the code actually exists.
Examples
Input
USMatches
US
Input
GBMatches
GB
Input
USANo match
—