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

234
Visualizações
How can I split a string on integer-comma substrings?

I want to split a string by commas but only with a number before comma. The number must be present in result groups.

It works fine until input string contains a comma without a number before, this comma should be a part of value group.

What should i change in the first regexp?

const doSplit = (parameter_type) => parameter_type.split(/(?!\d+)\,\s/).map((option) => {
      const matches = option.match(/(?<value>.+)\s(?<key>\d+)$/)
      if(matches === null) {
        return {Error: {option, parameter_type}}
       }
      return matches.groups
 })
 
 const list_a = "No reaction 0, Up 1, Down 3, Stop 5, Position 1 6, Position 2 7, Position 3 8, Position 4 9, Individual position 10, Enable automatic sun protection 14"
 
 console.log(doSplit(list_a))
 
 const list_b = "End position, no overflow 0, End position + 2% overflow 2, End position + 5% overflow 5, End position + 10% overflow 10, End position + 20% overflow 20, Total travel time + 10% Overflow 255"
 console.log(doSplit(list_b))
 

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

0

You could do this with just one regex, and matchAll:

const regex = /\s*,?\s*(?<value>.+?)\s+(?<key>\d+)(?=,|$)/g;
const doSplit = (parameter_type) => 
    Array.from(parameter_type.matchAll(regex), ({groups}) => groups);
 
 const list_a = "No reaction 0, Up 1, Down 3, Stop 5, Position 1 6, Position 2 7, Position 3 8, Position 4 9, Individual position 10, Enable automatic sun protection 14"
 console.log(doSplit(list_a))
 
 const list_b = "End position, no overflow 0, End position + 2% overflow 2, End position + 5% overflow 5, End position + 10% overflow 10, End position + 20% overflow 20, Total travel time + 10% Overflow 255"
 console.log(doSplit(list_b))

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