/^/
EasyText Processing3 min read

Tab Character Regex

Matches one or more consecutive tab characters, useful for detecting or converting tab-indented text.

#whitespace#text-processing#tabs#formatting

Regex Pattern

\t+

Default flags: g

Pattern Breakdown

Hover over a token to see what it does.

\t+
TokenMeaning
\tMatches a single horizontal tab character (U+0009)
tThe letter following the backslash; together with it, forms the \t tab escape sequence
+Requires one or more consecutive tab characters to be matched as a single run
\t+The complete pattern: a run of one or more tab characters matched as a single unit

Detailed Explanation

What it does

This pattern finds runs of one or more tab characters in a string, distinct from spaces or other whitespace. With the global flag, every tab run in the input is located.

Why it works

\t is the standard regex escape for the horizontal tab character across every mainstream engine, so it precisely targets tabs without matching spaces or newlines. The + quantifier groups consecutive tabs, which is convenient when converting tab-indentation to spaces or detecting mixed-indentation files.

Common use cases

  • Converting tab-indented source files to space-indented ones
  • Detecting mixed tabs-and-spaces indentation as part of a linting or formatting check
  • Splitting tab-separated values (TSV) rows into fields
  • Stripping stray tab characters from copy-pasted text before display

Edge cases

  • A single tab, like 'a\tb', matches since + allows a run of exactly one
  • Multiple consecutive tabs, like 'a\t\tb', are matched as a single combined run
  • A string with only spaces and no tabs never matches
  • A tab at the very start or end of a string is matched the same as one in the middle

Limitations

  • \t only matches the ASCII horizontal tab; it does not match vertical tabs (\v) or other whitespace
  • It cannot on its own determine the intended indentation width a tab represents, since that's an editor/rendering concern
  • Replacing tabs with a fixed number of spaces via this regex alone doesn't account for tab stops aligning to varying column positions

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

a b a b
3 matches

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const tabsRegex = /\t+/g;
const spaced = 'col1\tcol2\tcol3'.replace(tabsRegex, '    ');

Common Mistakes

Typing a literal tab character in the pattern source instead of the \t escape, which is invisible and error-prone in source files

Fix: Always use the \t escape sequence so the pattern is readable and portable across editors

Confusing \t with \s when the goal is specifically to detect or convert tabs, not all whitespace

Fix: Use \t when tabs specifically matter (e.g. indentation style checks); use \s for general whitespace

Replacing tabs with a fixed string without considering tab stop alignment in rendered output

Fix: If visual alignment matters, calculate spaces based on the current column rather than a flat replacement

Performance Notes

  • \t+ is a simple, cheap pattern with no backtracking concerns since it matches a single fixed character repeated
  • Global replacement of tabs across large files remains linear in the size of the input
  • For structured tab-separated data, a dedicated TSV parser is more robust than ad hoc regex splitting

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes