DD
DevDash

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

InputResult
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"
end

php

if (preg_match('/\s{2,}/', $value)) {
    echo "valid";
}

java

String pattern = "\\s{2,}";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.