Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
What’s the best way to extract the State code from all types of addresses?

I’m trying to figure out how to extract the State code from any type of address.

“123 Sunny Dr., Los Angeles, CA 90210, US”
“123 Sunny Lane, Unit B, Los Angeles, CA 90210, USA”
“123 Sunny Ct, Hollywood, CA, USA”
“Los Angeles, CA, US”

For any of these, I would just like the return value to be “CA”

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I would use match here with an alternation containing all US state codes:

var addresses = ["123 Sunny Dr., Los Angeles, CA 90210, US", "123 Sunny Lane, Unit B, Los Angeles, CA 90210, USA", "123 Sunny Ct, Hollywood, CA, USA", "Los Angeles, CA, US"];
var states = addresses.map(x => x.match(/\b(?:AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|PR|RI|SC|SD|TN|TX|UT|VT|VA|VI|WA|WV|WI|WY)\b/));
for (var i=0; i < addresses.length; ++i) {
    console.log(addresses[i] + " => " + states[i]);
}

We could try to formulate the state code as being a two letter uppercase letter word, but that runs the risk of matching US, which isn't a state code, but rather a country code. A hard-coded alternation works here as there are less than 100 total state/territory codes for the US.

about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming you're getting just one string, you can

  • create a function that takes a string as an argument
  • split the passed in string on "," into an Array
  • get the second last value in the Array into a variable
  • remove whitespace and numbers, using a regex into another variable
  • return this last variable
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda