Files
30-seconds-of-code/snippets/expandTabs.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

488 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
expandTabs string,regexp,beginner 2020-06-01T17:05:39+03:00 2020-09-15T16:28:04+03:00

Convert tabs to spaces, where each tab corresponds to count spaces.

  • Use String.prototype.replace() with a regular expression and String.prototype.repeat() to replace each tab character with count spaces.
const expandTabs = (str, count) => str.replace(/\t/g, ' '.repeat(count));
expandTabs('\t\tlorem', 3); // '      lorem'