Files
30-seconds-of-code/snippets/expandTabs.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

536 B

title, tags, author, cover, firstSeen, lastUpdated
title tags author cover firstSeen lastUpdated
Expand tabs into spaces string,regexp chalarangelo houses-rock-sea 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'