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
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).
Python (re) code
import re
pattern = re.compile(r"[ \t]+$", re.MULTILINE)
input_text = "good line\nbad line \nfine"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)[ \t]+$`)
input := `good line\nbad line \nfine`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
[ \t]+$ (flags: gm)Raw source: [ \t]+$
How it works
Examples
Input
good line\nbad line \nfineMatches
Input
tab here\tand more\t\tMatches
\t\t
Input
no trailing spaceNo match
—Common use cases
- •Linter / formatter rules (eslint, prettier, gofmt)
- •Pre-commit hooks for cleanup
- •Markdown source hygiene
- •Git diff noise reduction
Related patterns
Whitespace Trim (Leading & Trailing)
Text ProcessingMatch leading and/or trailing whitespace on a string — the regex equivalent of .trim().
Whitespace-Only Line
Text ProcessingMatches lines containing only whitespace (or empty lines).
Single-Line Comment (// or #)
Text ProcessingMatch single-line comments using either the `//` (C-family) or `#` (shell, Python, Ruby, YAML) marker.
Emoji (Unicode)
Text ProcessingMatch emoji characters across the main Unicode emoji ranges — requires the Unicode flag in JavaScript.