Expand tabs into spaces

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'

Written by Angelos Chalaris

I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.

If you want to keep in touch, follow me on GitHub or Twitter.

More like this

  • HSL to object

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

    JavaScript, String · Oct 22, 2020

  • RGB to object

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

    JavaScript, String · Oct 22, 2020

  • Compact whitespaces

    Compacts whitespaces in a string.

    JavaScript, String · Oct 18, 2020