Skip to main content
Regexflux

Line Indentation

beginner

Captures the indentation level of each line.

whitespaceindentationformattingcode

Pattern

/^([ \t]+)/gm

Try It

0 of 5 strings matched

Explanation

Captures leading spaces and tabs at the start of each line. Group 1 contains the indentation string for measuring indent level.

Test Strings

Matching

  • two spaces
  • one tab
  • four spaces

Non-matching

  • no indent

Language Compatibility

LanguageSupport
JSFull support
PYTHONFull support
JAVAFull support
PHPFull support
GOFull support

Code Snippets

const regex = /^([ \t]+)/gm;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);

Related Patterns