Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Regex match to check US State Code exist in string

I am large set of array from which I am trying to find the address

For example this(State Code :- CO)

0 Site Address, San Luis, CO 81152

Now to match this I am using this regex expression :-

e.match(/, CO \d/)

How can I incorporate many statecode in it like if I have to check for this(State Code :- NM)

2 X Valley Rd X P2, Questa, NM 87556

Any address state code can come, so I want it to dynamic to check all the state codes

Please suggest

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The most basic way to capture any state code would be to include a capture group with all the values you need to match.

Ex:

e.match(/, (CO|NM|TN|MO) \d/)

Wrap the different state options in parentheses, and use the pipe symbol to indicate "or".

If you have all the state codes in an array you could dynamically create this capture group.

ex:

const states = [
  "TN",
  "NM",
  "CO",
]

const regexStr = states.join("|")
const stateMatch = new RegExp(`, (${regexStr}) \d`)

e.match(stateMatch)
about 4 years ago · Juan Pablo Isaza Report

0

You can try with the following regex:

, [A-Z]{2} \d{5}

It checks for symbols in the following succession:

  • , : a comma + a space
  • [A-Z]{2}: two letters
  • : a space
  • \d{5}: five digits

This approach will generalize on the state codes.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!