Regex To Remove Unnecessary Spaces In A String

Category: Strings

A regular expression to match unnecessary whitespaces in a string (excluding spaces between words). Can be useful in removing all unexpected white spaces at the beginning or end of strings.

/^\s+|\s+$|\s+(?=\s)/

Matches:

  • RegexPattern<SPACE>
  • <SPACE>RegexPattern

Non-matches:

  • Regex<SPACE>Pattern

See Also:

Rate This Regex
Sending
User Review
0 (0 votes)

Expression Flags

Flags Description
i Ignore case sensitive.
g Allows global search.
m Allows multiline search.