JavaScript RegExp \t Metacharacter

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Horizontal tab

What You’ll Learn

The \t metacharacter matches a horizontal tab (HT, U+0009)—the character inserted when you press Tab. It is essential for parsing TSV files, cleaning pasted spreadsheet data, and handling column-aligned text.

01

Tab char

\t

02

U+0009

HT control

03

TSV

split(/\t/)

04

In \s

Whitespace

05

Replace

Tabs → spaces

06

vs space

Not the same

Introduction

When you copy data from a spreadsheet or export a TSV file, columns are often separated by tab characters rather than commas. The \t metacharacter lets your regex target those separators precisely.

Unlike spaces, a tab is a single control character. In regex, /\t/ matches exactly one tab—not multiple spaces and not other whitespace types unless you use the broader \s class.

Understanding the \t Metacharacter

\t matches the horizontal tab character (Unicode U+0009). In JavaScript string literals, "\t" creates the same character that /\t/ matches in a regex.

JavaScript
const row = "name\tage\tcity";

/\t/.test(row);           // true
/\t/.test("hello world"); // false (space, not tab)

row.split("\t");          // ["name", "age", "city"]

Tab is included in \s but is not the same as a literal space. Use \t when you need tab-specific parsing; use \s when any whitespace separator is fine.

💡
Beginner Tip

To parse a TSV row, write line.split(/\t/). Each array element is one column value.

How to Use \t in JavaScript

Use \t in detection, splitting, replacement, and validation workflows:

JavaScript
/\t/.test("a\tb");                 // true
"one\ttwo\tthree".split(/\t/);   // ["one", "two", "three"]
"col1\tcol2".replace(/\t/g, ", "); // "col1, col2"
!/^\t/.test("  hello");            // true (no leading tab)

Pair \t with the global flag for bulk replacement, or combine with \n when parsing multi-line TSV files.

📝 Syntax

JavaScript
\t              // one horizontal tab
\t+             // one or more tabs
/\t/g           // global: every tab
split(/\t/)      // split columns
\s              // any whitespace (includes \t)

Common patterns

  • /\t/ — detect whether a tab character exists.
  • split(/\t/) — split a TSV row into columns.
  • /\t/g with replace(", ", ...) — convert tabs to comma-space.
  • /^\t+/ — detect leading indentation tabs.

⚡ Quick Reference

GoalPattern
One tab\t
Split TSV columnssplit(/\t/)
Replace all tabs/\t/g
Any whitespace\s
Space only (literal)
Code pointU+0009 (HT)

📋 \t vs space vs \s

Three related patterns for tab and whitespace handling.

Tab only
\t

Horizontal HT

Space
 

U+0020 only

Whitespace
\s

Tab + space + more

Many tabs
\t+

Quantifier

Examples Gallery

Open DevTools Console (F12) or use Try-it links. Examples cover detection, TSV parsing, tab replacement, leading-tab checks, and multi-line files.

📚 Getting Started

Detect whether a string contains a tab character.

Example 1 — Test for a Tab Character

Return true when a horizontal tab appears in the text.

JavaScript
console.log(/\t/.test("hello"));       // false
console.log(/\t/.test("a\tb"));        // true
console.log(/\t/.test("hello world")); // false (space only)
Try It Yourself

How It Works

A single \t matches one tab character. Strings with only spaces do not satisfy the pattern because space (U+0020) is a different code point.

📈 Practical Patterns

Parse, replace, and validate tab-separated data.

Example 2 — Split a TSV Row Into Columns

Parse tab-separated values from a single line of data.

JavaScript
const row = "Alice\t28\tEngineer";

const cols = row.split(/\t/);
console.log(cols); // ["Alice", "28", "Engineer"]

console.log(cols[0]); // "Alice"
console.log(cols[2]); // "Engineer"
Try It Yourself

How It Works

split(/\t/) breaks the string at each tab boundary. Empty fields between consecutive tabs produce empty strings in the result array.

Example 3 — Replace Tabs With Spaces

Convert tab separators to spaces for display or further processing.

JavaScript
const tsv = "name\tage\tcity";

const spaced = tsv.replace(/\t/g, " ");
console.log(spaced); // "name age city"

const csv = tsv.replace(/\t/g, ", ");
console.log(csv);    // "name, age, city"
Try It Yourself

How It Works

The global flag with /\t/g replaces every tab in the string. Useful when normalizing pasted spreadsheet data.

Example 4 — Detect Leading Tab Indentation

Check whether a line starts with tab characters (common in code blocks).

JavaScript
const indented = "\t\tfunction hello() {}";
const normal = "  function hello() {}";

console.log(/^\t+/.test(indented)); // true
console.log(/^\t+/.test(normal));    // false (spaces, not tabs)

const level = indented.match(/^\t+/)[0].length;
console.log(level); // 2
Try It Yourself

How It Works

^\t+ anchors to the start and matches one or more tabs. Space-based indentation does not match because spaces are not tab characters.

Example 5 — Parse a Multi-Line TSV String

Split a small TSV file into rows and columns.

JavaScript
const tsv = "name\tscore\nAlice\t95\nBob\t88";

const rows = tsv.split(/\n/).map(function (line) {
  return line.split(/\t/);
});

console.log(rows);
// [["name","score"], ["Alice","95"], ["Bob","88"]]
Try It Yourself

How It Works

First split on \n for rows, then split each line on \t for columns. See the \n tutorial for newline details.

🚀 Common Use Cases

  • TSV parsing — split exported spreadsheet rows with split(/\t/).
  • Data normalization — replace tabs with spaces or commas for CSV conversion.
  • Paste cleanup — detect tabs in user-pasted content from Excel or Sheets.
  • Code indentation — count leading tabs with /^\t+/.
  • Log parsing — handle tab-delimited log formats.
  • Validation — reject fields that must not contain tab characters.

Important Considerations

  • Not spaces\t matches tab only; literal spaces require or \s.
  • Part of \s/\s+/ also splits on tabs, but mixes all whitespace types.
  • Empty fields — consecutive tabs in TSV produce empty strings in split results.
  • Global flag — omitting g in replace() changes only the first tab.
  • Not vertical tab\t is horizontal tab; \v is a different character.

🧠 How \t Matches Tabs

1

Read character

The engine inspects the next character at the current position.

Scan
2

Check U+0009

\t succeeds only if the character is a horizontal tab (HT).

Match
3

Apply quantifier

With + or global replace, the engine repeats the tab check.

Extend
4

Continue pattern

Tab matched → advance. Other char → fail or backtrack.

📝 Notes

  • \t is horizontal tab (U+0009, ASCII 9).
  • Tab is included in the \s whitespace class.
  • Review \s for general whitespace and \n for line breaks.
  • Next up: \uXXXX Unicode escape metacharacter.
  • For TSV files, split rows on \n first, then columns on \t.
  • Do not confuse \t (horizontal) with \v (vertical tab).

Browser & Runtime Support

The \t tab metacharacter is part of core JavaScript regular expression syntax.

Baseline · ES3+

RegExp \t

Supported in every browser and Node.js version. \t matches the horizontal tab control character.

99% Universal syntax
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
\t Excellent

Bottom line: Safe everywhere. Use \t when you need tab-specific parsing beyond general \s whitespace.

Conclusion

The \t metacharacter matches the horizontal tab character (U+0009). It is essential for parsing TSV data, cleaning pasted spreadsheet content, and detecting tab-based indentation.

Use split(/\t/) for columns, /\t/g with replace() to normalize separators, and remember that tab is not the same as a space. Next, learn the \uXXXX Unicode escape metacharacter.

💡 Best Practices

✅ Do

  • Use split(/\t/) for TSV column parsing
  • Use /\t/g to replace all tabs at once
  • Combine \n + \t for multi-line TSV
  • Use \t when tabs specifically matter
  • Use \s when any whitespace separator is acceptable

❌ Don’t

  • Assume spaces and tabs are interchangeable
  • Split on space when data uses tab delimiters
  • Forget the global flag when replacing all tabs
  • Confuse \t with \v (vertical tab)
  • Expect visual column alignment to equal character count

Key Takeaways

Knowledge Unlocked

Five things to remember about RegExp \t

Match the horizontal tab character for TSV and column data.

5
Core concepts
📈 02

U+0009

HT code.

Unicode
🚫 03

split()

TSV cols.

Parse
🔒 04

/\t/g

Replace.

Normalize
🔢 05

\s

Includes tab.

Related

❓ Frequently Asked Questions

\t matches the horizontal tab character (HT, Unicode U+0009, ASCII 9). It is the control character produced by the Tab key in most text editors.
No. A tab is a single character with a fixed code point. Visually it may align to column stops, but in regex it matches exactly one tab—not a run of space characters.
Yes. Horizontal tab is included in the \s whitespace class along with space, newline, carriage return, form feed, and vertical tab.
Use split with a tab pattern: row.split(/\t/). For a full file, split lines first with /\n/, then split each line on /\t/.
\t is horizontal tab (moves to the next tab stop on the same line). \v is vertical tab (a different, rarely used control character).
The \t escape is core regex syntax since ES3. It works in every browser and Node.js version.
Did you know?

TSV (tab-separated values) is a common export format from spreadsheets. While CSV uses commas, TSV uses \t as the column delimiter—making split(/\t/) the natural parsing approach in JavaScript.

Continue to \uXXXX Unicode metacharacter

Learn how Unicode escape sequences match characters in modern JavaScript regex.

\uXXXX tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful