Regular Expressions 101

Enterprise

Community Library

Community Library Entry

2

Regular ExpressionOpen Workspace

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

Description
Created·2025-03-10 16:45
Type·Substitution
Flavor·PCRE2 (PHP)

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
Open Workspace