JavaScript / ECMAScript

Tab Character in JS

Match literal tab characters — the regex behind every formatter / linter that yells about indentation.

Try it in the JS tester →

Pattern

regexJS
\t   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\t", "g");
const input = "  spaces\\there";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

\t is the escape for the tab character (U+0009). The g flag finds every occurrence. Pair with `replace(re, ' ')` for tabs-to-spaces conversion or use it as a detector to flag mixed indentation.

Examples

Input

spaces\there

Matches

  • \t

Input

\t\tdouble tab

Matches

  • \t
  • \t

Input

no tabs at all

No match

Same pattern, other engines

← Back to Tab Character overview (all engines)