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

122
Views
How to convert JS multi-line string to single-line with tabs

I want to take a string, such as

const code = `
<code>
    This is some code
</code>
`

and convert it automatically to a one line string, using \n for breaks and \t for tabs.

After conversion it should look like:

const code = "\n\tThis is some code\n"

How can this be done in JavaScript? I saw posts solving how to add line breaks, but found nothing for tab support.

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

0

one solution could be to find 4 spaces and replace it by \t like this:

const code = `
<code>
    This is some code
</code>
`;
const result = code.replace(/ {4}/g, '\\t').replace(/<\/*code>\n?/g, '').replace(/\n/g, '\\n');
console.log(result);

since OP has said result should be \n\tThis is some code\n, I've also replaced <code></code> with empty string.

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!