Regular Expressions 101

Community Patterns

There does not seem to be anything here

Community Library Entry

2

Regular Expression
PCRE2 (PHP >=7.3)

/
^\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 - 2 months ago