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

195
Vistas
.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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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