Regex for Consecutive Spaces
Regex Pattern
\s{2,}Two or more consecutive whitespace characters
Quick Answer
The regex pattern for consecutive spaces is `\s{2,}`. Two or more consecutive whitespace characters. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| hello world | ✓ Matches |
| too many spaces | ✓ Matches |
| tab here | ✓ Matches |
| hello world | ✗ No match |
| single space | ✗ No match |
| nospaces | ✗ No match |
Code Examples
javascript
const regex = /\s{2,}/;
const isValid = regex.test(value);python
import re
pattern = r'\s{2,}'
if re.match(pattern, value):
print("valid")ruby
pattern = /\s{2,}/
if value =~ pattern
puts "valid"
endphp
if (preg_match('/\s{2,}/', $value)) {
echo "valid";
}java
String pattern = "\\s{2,}";
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)
Trailing Whitespace
Whitespace at the end of a line (useful for linting)
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