0

I need to accept a string that define how many ZIP code the user will use.

Examples:

  • If the user wants to use all zip codes from 1000 to 1200
    Then he will enter 1000:1200
  • If he wants to use only 1000 and 1200
    Then he will enter 1000,12000
  • combining both is possible 1010:1015,1019,1025:1027 will return [1010,1011,1012,1013,1014,1015,1019,1025,1026,1027]

I need to verify that string.
I used this to verify but it didnt work

/(\d{4}(:|,){0,1}\d{4})/gm
5
  • 1
    zip codes are allways 4 digits? Commented Feb 1, 2022 at 17:42
  • Please clarify the common understanding of "ZipCode" is a U.S. postal code which is always 5 digits. Do you mean the Zip+4 the 4 part? like 69103-0123? Commented Feb 1, 2022 at 22:05
  • 1
    "Then he will enter 1000,12000" um 12000 is more than 1200 Commented Feb 1, 2022 at 22:06
  • Your question is unclear... Do you need to validate only? What if I use 1200:1000? or 1200:1000,1500Have you thought about that? How do you plan to validate that using regex? Commented Feb 1, 2022 at 22:45
  • First of, to validate a range 1000 to 1200 you should use 1(?:[01]\d{2}|200) Commented Feb 1, 2022 at 22:51

3 Answers 3

2

Start with a basic phrase to match either #### or ####:####

\d{4}(:\d{4})?

Then extend it to match any number of them in a comma-separated list:

\d{4}(:\d{4})?(,\d{4}(:\d{4})?)*

Finally, surround with ^ and $ to validate the entire line.

^(\d{4}(:\d{4})?)(,(\d{4}(:\d{4})?))*$
Sign up to request clarification or add additional context in comments.

Comments

0

You need to expand these matches after the match is found. I assume the string is validated before, if not use if (/^\d+(?:[:,]\d+)*$/.test(s)) { ... }.

let s = '1010:1015,1019,1025:1027';
const chunks = s.replace(/(\d+):(\d+)/g, (_,$1,$2) =>
    Array.from(
        new Array(Number($2)-Number($1)), 
        (x, i) => 
            i + Number($1))
    .join(",")).split(',');
console.log(chunks);

The Array.from(new Array(Number($2)-Number($1)), (x, i) => i + Number($1)) part generates a range of numbers between Group 1 and Group 2 values obtained with the /(\d+):(\d+)/g regex.

Comments

0

As much as I tried to simplify a Regex, here's a true validator for your specific strings.
Looks a bit long since it matches a range 1000→1200: 1(?:[01]\d{2}|200)

/^1(?:[01]\d{2}|200)(?::1(?:[01]\d{2}|200)(?!:)|,1(?:[01]\d{2}|200))*$/g

Regex101.com example and description

Without the range
the Regex is simpler but it will also match any four digit number like 0000, 9999:

/^\d{4}(?::\d{4}(?!:)|,\d{4})*$/g

Regex101.com example and description

Simplest - but not to be used a validator
The simpler solution would be

/^\d{4}(?:[,:]\d{4})*$/g

Regex101.com example and description
but as you can see it will match many wrong inputs like the invalid 1000:1005:1020 (two :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.