Languages
[Edit]
EN

JavaScript - add tabulator to each one line in text

9 points
Created by:
Selina-Miranda
337

In this short article, we would like to show how to add a tabulator to each one line in text using JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const text = `
abc
123
`;

const prefix = '\t\t';  // 2 tabulators

const result = text
	.split(/\r\n|\n\r|\n|\r/)
	.map(line => prefix + line)
	.join('\n');

console.log(result);

See also

  1. JavaScript - split string by new line character 
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join