QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Convert Tabs to Spaces

Converts all tab characters in a string to spaces.

  • string
  • tabs
  • spaces

Install

npx shadcn@latest add https://quicksnip.dev/r/convert-tabs-to-spaces.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

function tabsToSpaces(str, spacesPerTab = 4) {
  return str.replace(/\t/g, ' '.repeat(spacesPerTab));
}

Usage

tabsToSpaces('Hello\tWorld', 2); // Returns: 'Hello  World'