Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
.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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!