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

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

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