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

143
Vistas
Split with Regex before sequence appears

I have a text file with following content:

Test, [636,13,"be738jsk","some, text",js]

I want to read this content into an Array. I currently use JavaScript with regex to split into substrings directly into an array. As regex i have: line.split(/,\s\[|",|,"|","/);

The problem is, i have some sentences like in the example with a "," in it and i don't want to split there. So i tried to say in the regex, "split after , and everthing except whitespace". The problem is, it also cuts out the "everything" after the ,

Example:

Test, [63737,33,"bla,blablba",737]

When i use this regex:

Line.split(/,"|,\s\[|",|,[^\s]/); Then it cut's out the 3 from 33 :(

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use

const text = 'Test, [636,13,"be738jsk","some, text",js], "[Blablba] Blablba"';
console.log(
  Array.from(text.matchAll(/\w+|"([^"]*)"/g), x => x[1] ?? x[0])
)

Output:

[
  "Test",
  "636",
  "13",
  "be738jsk",
  "some, text",
  "js",
  "[Blablba] Blablba"
]

Here, \w+|"([^"]*)" matches one or more word chars or any zero or more chars other than double quotation marks (captured into Group 1) in between double quotation marks. The x => x[1] ?? x[0] part takes Group 1 values if defined, else, it keeps the whole match value.

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