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

215
Visualizações
Decipher a couple of things in expand ranges function

Trying to get some zip code expansion js code, I got the following:

function parse(string) {
    const numbers = [];
    for (const [, beginStr, endStr] of string.matchAll(/(\d+)(?:-(\d+))?/g)) {
        const [begin, end] = [beginStr, endStr].map(Number);
        numbers.push(begin.toString().padStart(5, "0"));
        if (endStr !== undefined) {
            for (let num = begin + 1; num <= end; num++) {
              
              numbers.push(num.toString().padStart(5, "0"));
          }
        }
    }
    return numbers;
}

There are a couple of things I don't understand. First off, why is there a comma when declaring beginStr and endStr in the for .. of loop? Two, the regex has a ?: that is explained like "? matches the previous token between zero and one times, as many times as possible, giving back as needed", but there is no previous token and the colon is not explained. Does anybody know how this works?

Thanks, Raf

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

0

The matchAll returns an Iterator with a value of [ZIP range, ZIP start, ZIP end]. Since the code does not need the ZIP range, you can skip that value when destructuring the array. So it's basically

const [
  , // ZIP range
  beginStr, // ZIP start
  endStr, // ZIP end
]

As for the ?: What is a non-capturing group in regular expressions?

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