Text Extraction
Trailing Whitespace Regex
Trailing spaces are invisible in most editors but show up as noise in code reviews and can break formats such as Markdown, where two spaces at the end of a line force a line break. This pattern matches one or more spaces or tabs that sit right before the end of a line. The m flag is essential: it makes the dollar anchor match at every line ending, not only at the end of the whole input. Run a global replace with an empty string to strip trailing whitespace from an entire file while leaving leading indentation and line breaks exactly as they were.
Pattern (JavaScript)
/[ \t]+$/mMatches one of the characters \t, one or more times, then the end of the line/string.
Examples
Matches
- ✓ line with space
- ✓ tabbed
- ✓ first second
Does not match
- ✗ clean line
- ✗ leading only
- ✗ a b c d
Try it
3 matches (JavaScript engine)
line with space tabbed first second clean line leading only a b c d
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /[ \t]+$/gm;
const text = "line with space ";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'[ \t]+$', re.M)
text = 'line with space '
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/[ \t]+$/m';
$text = 'line with space ';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("[ \\t]+$", Pattern.MULTILINE);
String text = "line with space ";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?m)[ \t]+$`)
text := "line with space "
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"[ \t]+$", RegexOptions.Multiline);
var text = "line with space ";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"(?m)[ \t]+$").unwrap();
let text = "line with space ";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /[ \t]+$/
text = 'line with space '
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matches- Flag(s) m not available in this language and were left out.
Frequently asked questions
- Why is the m flag required?
- Without m, the $ anchor only matches at the very end of the input. With m it also matches before each newline, so every line is checked.
- Will this also remove Windows carriage returns?
- No. The class only contains space and tab. If your file uses CRLF line endings, use [ \t]+(?=\r?$) or normalise line endings first.
- Is trailing whitespace ever meaningful?
- Yes. In Markdown, two trailing spaces create a hard line break. Check your format before stripping it blindly.