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

109
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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