This regex pattern matches American zip codes. It starts with a word boundary \b, followed by 5 digits \d{5}. It then optionally matches a hyphen and 4 digits (?:-\d{4}) before ending with another word boundary \b.
const regex = /\b\d{5}(?:-\d{4})?\b/;
const zipCode = '12345';
console.log(regex.test(zipCode)); // Output: true
Copiar