Text Processingflags: gm

Trailing Whitespace (Per Line)

Match trailing spaces and tabs at the end of each line — the regex linters use to flag dirty whitespace.

Try it in RegexPro →

Available in

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("[ \\t]+$", "gm");
const input = "good line\\nbad line  \\nfine";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

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

Pattern

regexengine-agnostic
[ \t]+$   (flags: gm)

Raw source: [ \t]+$

How it works

[ \t]+ matches one or more space or tab characters. $ with the m (multiline) flag anchors to the end of EACH line, not the whole string. The g flag iterates so a multi-line string surfaces every offending line. Pair with `replace(re, '')` to strip.

Examples

Input

good line\nbad line \nfine

Matches

Input

tab here\tand more\t\t

Matches

  • \t\t

Input

no trailing space

No match

Common use cases

  • Linter / formatter rules (eslint, prettier, gofmt)
  • Pre-commit hooks for cleanup
  • Markdown source hygiene
  • Git diff noise reduction

Related patterns