652 B
652 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Indent string | snippet | javascript |
|
metropolitan-window | 2020-11-01T20:50:57+02:00 |
Indents each line in the provided string.
- Use
String.prototype.replace()and a regular expression to add the character specified byindentcounttimes at the start of each line. - Omit the third argument,
indent, to use a default indentation character of' '.
const indentString = (str, count, indent = ' ') =>
str.replace(/^/gm, indent.repeat(count));
indentString('Lorem\nIpsum', 2); // ' Lorem\n Ipsum'
indentString('Lorem\nIpsum', 2, '_'); // '__Lorem\n__Ipsum'