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

104
Vistas
Regex Match splitting simple words and quotation words by comma

im new to regular expressions and i want to build an expression that finds the below pattern:

I have the string:

"'Hello world',dude, 'Somethings, never, turn and go', bye" 

I want a regular expression that gives this result:

['Hello world',dude,'Somethings, never, turn and go',bye]

Basically, splitting the string on comma but keeping the phrases with quotes that have comma as a whole.

I have this regex:

let s = "'Hello world',dude, 'Somethings, never, turn and go', bye";
let arr = s.split(/(?<=')\s*,\s*|\s*,\s*(?=')/g);
console.log(arr)

If i add this string "'Hello world',dude, Somethings, never, turn and go, bye" it gives this result ["'Hello world'", 'dude, Somethings, never, turn and go, bye'] which is wrong. It doesnt split the other values separated by comma. How do i fix that?

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

0

You can use split with a capture group to keep the split values for '...' and using an alternation to split on , between optional whitespace chars.

As split can give empty items in the resulting array, you can remove those with filter(Boolean);

Also matching escaped single quotes:

('[^'\\]*(?:\\.[^'\\]*)*')|\s*,\s*

See a regex demo.

let s = "'Hello world',dude, 'Somethings, never, turn and go', bye";
let arr = s.split(/('[^'\\]*(?:\\.[^'\\]*)*')|\s*,\s*/g).filter(Boolean);
console.log(arr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

I seen your previous question. Coincidentally it showed comma's right after or before the single quote. Hence the "wrong" answer back there. In this case, maybe look for balanced single quotes ahead:

let s = "'Hello world',dude, Somethings, never, turn and go, bye";
let arr = s.split(/\s*,(?=(?:[^']*'[^']*')*[^']*$)\s*/);
console.log(arr)

See an online demo.

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