Regular Expressions 101

X-Trim (All-in-One Trimmer - Space Sanitizer)

Open Workspace
  • PCRE2 (PHP)
  • Substitution
2

Regular Expression

/
^\s+|\s+$|(\s)\s+
/
gm

Description

Regex Pattern ^\s+|\s+$|(\s)\s+

Replacement $1

Description This regex pattern performs multi-purpose whitespace normalization by:

  • Removing leading whitespace (^\s+)
  • Trimming trailing whitespace (\s+$)
  • Collapsing multiple consecutive whitespace characters into a single space ((\s)\s+ → \1) The replacement \1 preserves the first captured whitespace character while eliminating redundant ones, ensuring clean, standardized spacing.
Submitted by Fernando Abritta