Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda