Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex for zip-code

Tags:

regex

People also ask

How do I verify a US ZIP Code?

US ZIP code (U.S. postal code) allow both the five-digit and nine-digit (called ZIP + 4) formats. E.g. a valid postal code should match 12345 and 12345-6789, but not 1234, 123456, 123456789, or 1234-56789. ^ # Assert position at the beginning of the string. [0-9]{5} # Match a digit, exactly five times.

Is ZIP Code a string or integer?

Well, ZIP codes are still not integers! ZIP codes for New England states all start with a "0." If your database stores its ZIP codes as integers, then those leading zeroes are stripped. That means a Boston ZIP code (02115) would incorrectly show up as a four-digit number (2115) instead.

What is US ZIP Code format?

A United States postal code, for example, might be either five digits or five digits followed a hyphen (dash) and another four digits (12345-1234). If you are validating US postal codes, allow for both conditions. The following rule validates a field that can be five digits, five digits + dash + 4 digits, or blank.


^\d{5}(?:[-\s]\d{4})?$
  • ^ = Start of the string.
  • \d{5} = Match 5 digits (for condition 1, 2, 3)
  • (?:…) = Grouping
  • [-\s] = Match a space (for condition 3) or a hyphen (for condition 2)
  • \d{4} = Match 4 digits (for condition 2, 3)
  • …? = The pattern before it is optional (for condition 1)
  • $ = End of the string.

For the listed three conditions only, these expressions might work also:

^\d{5}[-\s]?(?:\d{4})?$
^\[0-9]{5}[-\s]?(?:[0-9]{4})?$
^\[0-9]{5}[-\s]?(?:\d{4})?$
^\d{5}[-\s]?(?:[0-9]{4})?$

Please see this demo for additional explanation.

If we would have had unexpected additional spaces in between 5 and 4 digits or a continuous 9 digits zip code, such as:

123451234
12345 1234
12345  1234

this expression for instance would be a secondary option with less constraints:

^\d{5}([-]|\s*)?(\d{4})?$

Please see this demo for additional explanation.

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Test

const regex = /^\d{5}[-\s]?(?:\d{4})?$/gm;
const str = `12345
12345-6789
12345 1234
123451234
12345 1234
12345  1234
1234512341
123451`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}