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

50
Views
Replace double backslashes into single backslashes to be processed by ansi-to-html

I'm trying to process a terminal output. Let's say it's the string "\x1b[01;32mREADME.md". It has a double backslash. If I run console.log("\\x1b[01;32mREADME.md"), it only shows a single backslash, but if I pass it to the toHtml from ansi-to-html, it doesn't work. And by work, I mean transforming the characters into html.

I tried several solutions involving replace or the fact that \ is U+5c in unicode but nothing worked. The options on the library don't help either.

codesandbox example

import Convert from "ansi-to-html";

export default function App() {
  const c = new Convert({ escapeXML: true });
  const single = "\x1b[01;32mREADME.md";
  const double = "\\x1b[01;32mREADME.md";

  console.log(double) // prints single backslash
  const res1 = c.toHtml(single);
  const res2 = c.toHtml(double);

  return (
    <div className="App">
      single backslash works:
      <pre>{single}</pre>
      <pre>{res1}</pre>
      <br />
      <br />
      double backslash doesnt work:
      <pre>{double}</pre>
      <pre>{res2}</pre>
    </div>
  );
}

Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use String.raw function:

const double = String.raw`\\x1b[01;32mREADME.md`;

With this you dont need to use convert:

export default function App() {
  const single = String.raw`\x1b[01;32mREADME.md`;
  const double = String.raw`\\x1b[01;32mREADME.md`;

  const res1 = single;
  const res2 = double;

  return (
    <div className="App">
      single backslash works:
      <pre>{single}</pre>
      <pre>{res1}</pre>
      <br />
      <br />
      double backslash doesnt work:
      <pre>{double}</pre>
      <pre>{res2}</pre>
    </div>
  );
}

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!