PatternRegex

Find trailing whitespace

Spot the invisible spaces and tabs clinging to line ends before they break diffs, fail linters, or bloat a file with silent whitespace.

Last updated

Patterngm
[ \t]+$
Open in Regex Tester & Builder

Matches

  • Hello world Goodbye
  • Tabs here: Next line

Doesn’t match

  • Hello world Goodbye
  • Clean line one Clean line two

Why it’s written this way

The character class [ \t] matches a literal space or a tab, and the trailing + requires one or more of either right before the line ends. Anchoring with $ pins the match to the end of a line rather than the end of the whole string.

The m flag is what makes $ mean 'end of each line' instead of 'end of the string' — drop it and this pattern would only ever check the very last line of a multi-line file.

Edge cases to know

  • It only catches ASCII space and tab — a non-breaking space (U+00A0) or other Unicode whitespace slips through untouched.
  • It flags a line's trailing run but says nothing about whitespace elsewhere on the line, such as double spaces mid-sentence.

Related in Patterns