Regex for Trailing Whitespace
Regex Pattern
\s+$Whitespace at the end of a line (useful for linting)
Quick Answer
The regex pattern for trailing whitespace is `\s+$`. Whitespace at the end of a line (useful for linting). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| trailing space | ✓ Matches |
| tab at end | ✓ Matches |
| mixed | ✓ Matches |
| no trailing | ✗ No match |
| clean line | ✗ No match |
Code Examples
javascript
const regex = /\s+$/; const isValid = regex.test(value);
python
import re
pattern = r'\s+$'
if re.match(pattern, value):
print("valid")ruby
pattern = /\s+$/ if value =~ pattern puts "valid" end
php
if (preg_match('/\s+$/', $value)) {
echo "valid";
}java
String pattern = "\\s+$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
Whitespace Only
Empty string or only whitespace characters
Leading/Trailing Whitespace
Matches whitespace at the start or end (for trimming)
Consecutive Spaces
Two or more consecutive whitespace characters
Email Address
Validates a standard email address format
Email (RFC 5322 Compliant)
RFC 5322 compliant email validation with label length limits
URL (HTTP/HTTPS)
Validates HTTP and HTTPS URLs