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

193
Visualizações
.exec() Regex only works when I console.log it

So I was trying to make a markdown parser that supports tailwinds but got stuck on the code block part. whenever I run .exec() on the code block regex I can't access the values from the returned array, I can only log the array to console. I also tried adding a lastIndex but that doesn't work.

function parseCodeBlock(data) {
  let match = /^(([ \t]*`{3,4})([^\n]*)([\s\S]+?)(^[ \t]*\2))/gm.exec(
    data.string
  );
  return JSON.parse(JSON.stringify(match));
}
// line 94 - 97
let match = parseCodeBlock({ string: HtmlCode });
console.log(match);
let code = match[3];

enter image description here

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

0

From the logs, your code is executed twice, the second time the returned value is null.

Note: I tried to add this as comment but I don't have enough reputation points as a new contributor :)

about 4 years ago · Juan Pablo Isaza Relatório

0

As I can see from your screenshot, there are two logs from the same code line (95). One with the array, the second with null.

I guess you're trying to access the array AFTER the second log (null)

about 4 years ago · Juan Pablo Isaza Relatório

0

I found another regex that fixed this problem here is the code.

function parseCodeBlock(data) {
  let html = data.string.replace(
    /^```(([a-z0-9-]+?)\n+)?\n*([^]+?)\n*```/gim,
    `<pre><code class="language-$1">$3</code></pre>`
  );
  return html;
}
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