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

205
Visualizações
Regex solution for matching groups does not work

Imagine a text like in this example:

some unimportant content

some unimportant content [["string1",1,2,5,"string2"]] some unimportant content

some unimportant content

I need a REGEX pattern which will match the parts in [[ ]] and I need to match each part individually separated by commas.

I already tried

const regex = /\[\[(([^,]*),?)*\]\]/g
const found = result.match(regex)

but it doesn't work as expected. It matches only the full string and have no group matches. Also it has a catastrophic backtracking according to regex101.com if the sample text is larger.

Output should be a JS array ["string1", 1, 2, 5, "string2"]

Thank you for your suggestions.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

What about going with a simple pattern like /\[\[(.*)\]\]/g and then you'd just have to split the result (and apparently strip those extra quotation marks):

const result = `some unimportant content

some unimportant content [["string1",1,2,5,"string2"]] some unimportant content

some unimportant content`;

// const found = /\[\[(.*)\]\]/g.exec(result);
const found = /\[\[(.*?)\]\]/g.exec(result); // As suggested by MikeM
const arr_from_found = found[1].replace(/\"/g, '').split(',');

console.log(arr_from_found); // [ 'string1', '1', '2', '5', 'string2' ]
about 4 years ago · Juan Pablo Isaza Relatório

0

Try replace method.

let cleantext = result.replace("[", "")

then

let more_cleantext = cleantext.replace("]", "")

but if your result variable is array then just

result[0]
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