Expand tabs into spaces

Angelos Chalaris · JavaScript, String, Regexp · Sep 15, 2020

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

const expandTabs = (str, count) => str.replace(/\t/g, ' '.repeat(count));
expandTabs('\t\tlorem', 3); // '      lorem'

Recommended snippets

  • HSL to object

    JavaScript, String

    Converts an hsl() color string to an object with the values of each color.

  • RGB to object

    JavaScript, String

    Converts an rgb() color string to an object with the values of each color.

  • Titlecase string

    JavaScript, String

    Converts a string to title case.