🔍Regex Recipes
Find Trailing Spaces
Detect trailing whitespace at end of lines for code cleanup.
Pattern
+$Explanation
Matches one or more spaces at the end of lines. Useful for code quality and linting.
Examples
Has trailing
Input
code
Output
✓ Match (3 trailing spaces)
Clean line
Input
code
Output
✗ No match
Tab trailing
Input
code
Output
✗ No match (space pattern only)
Code Examples
JavaScript
// Find lines with trailing spaces
const code = `
function hello() {
return 'world';
}
`;
// Check for trailing spaces
const hasTrailing = / +$/m.test(code);
// Remove trailing spaces
const cleaned = code.replace(/ +$/gm, '');
// Or all trailing whitespace (spaces, tabs)
const cleanedAll = code.replace(/\s+$/gm, '');Try it Now
💡 Tips
- Configure your editor to remove trailing spaces on save
- Add to pre-commit hooks
- Use \s+$ to include tabs
- Many linters flag this automatically
⚠️ Common Pitfalls
- Pattern only matches spaces, not tabs
- Use \s+ to match all whitespace types
- IDE settings can auto-remove trailing spaces
- May be caught by linters (ESLint, etc.)