Regex Japanese postal code
The Japan Post manual writes the code as 7 digits with a hyphen between the 3rd and 4th ('100-0001'), ASCII digits only. On an address it is preceded by the 〒 symbol, which the manual does not mention. None of the three real attempts handles both: validator.js requires the hyphen and rejects '1000001', a Kuroco rule is not anchored so 'abc100-0001xyz' passes, and django-localflavor accepts both spellings but rejects '〒100-0001'. Here's an anchored pattern accepting an optional hyphen and an optional 〒, compared against those three real attempts.
/^(?:〒 ?)?[0-9]{3}-?[0-9]{4}$/passes 26/26 cases
Comparison of existing patterns
| Source | pattern | Score on the corpus | ReDoS |
|---|---|---|---|
| validator.js — tiret obligatoire : rejette « 1000001 » ; rejette aussi « 〒100-0001 » | ^\d{3}\-\d{4}$ | 23 / 26 | safe |
| Kuroco (FAQ officielle) — non ancrée : « abc100-0001xyz » et « 100-00011 » passent ; rejette « 1000001 » sans tiret | [0-9]{3}-[0-9]{4} | 21 / 26 | safe |
| django-localflavor (JPPostalCodeField) — accepte 7 chiffres avec ou sans tiret, mais rejette « 〒100-0001 » ; en Python, « \d » accepte aussi les chiffres pleine chasse, que la Poste japonaise exclut | ^\d{3}-\d{4}$|^\d{7}$ | 24 / 26 | safe |
Implementation
const re = new RegExp("^(?:〒 ?)?[0-9]{3}-?[0-9]{4}$", "");
re.test(input);Limits
- Valide seulement la FORME (7 chiffres, tiret facultatif après le 3e) : ne vérifie pas qu'un code est réellement attribué par la Poste japonaise. « 000-0000 » passe, alors qu'il n'est très probablement attribué à aucune adresse.
- Le tiret facultatif et le préfixe « 〒 » sont des tolérances de saisie : le manuel de la Poste japonaise ne décrit que la forme avec tiret et ne mentionne pas « 〒 ». Si vous stockez le code, normalisez-le en « NNN-NNNN » sans 〒.
- Rejette les chiffres pleine chasse (« 100-0001 ») : le manuel dit « 当分の間、算用数字のみ » (chiffres arabes seulement, pour l'instant). C'est un choix de la Poste, pas une loi : convertissez-les en ASCII avant validation si votre public saisit en japonais.
- Un code à 7 chiffres ne dit rien de l'adresse : seul un fichier de correspondance code-adresse comme celui de la Poste japonaise confirme qu'un code existe et à quelle zone il correspond.
Postal codes for other countries
- Argentine postal code (CPA)
- Australian postcode
- Brazilian postal code (CEP)
- Canadian postal code
- Czech postal code (PSČ)
- Dutch postal code
- French postal code
- German postal code (PLZ)
- Indian PIN code
- Irish Eircode
- Polish postal code
- Portuguese postal code
- Spanish postal code
- Swedish postal code
- Swiss postal code (PLZ/NPA)
- UK postcode
- US ZIP code (ZIP and ZIP+4)
- All postal codes by country