Whitespace Normalization
Match leading, trailing, and consecutive internal whitespace for normalization.
What Is This?
This regex pattern matches three types of whitespace for text normalization: leading whitespace (^\s+), trailing whitespace (\s+$), and consecutive internal whitespace (\s+(?=\s)). When you replace matches with an empty string (for leading/trailing) or a single space (for internal), you get clean, normalized text.
How to Use
The Pattern
Use this pattern with String.replace() to normalize whitespace. First replace the internal matches (consecutive spaces) with a single space, then trim leading/trailing whitespace. Alternatively, use .trim() and .replace(/\s+/g, ' ') for the same result.
/^\s+|\s+$|\s+(?=\s)/
Examples
Whitespace issues
Matches: Hello World Too many spaces Does not match: normal text hello world single spaces only
Edge cases
Matches: Leading tab Trailing newline Mixed whitespace Does not match: already-normalized-text
Related Entries
More from this reference:
Frequently Asked Questions
Is this better than JavaScript's .trim()?
For simple trimming, .trim() is faster and more readable. This regex is useful when you need to handle all three types of whitespace issues (leading, trailing, internal) in a single pass or when using regex-based tools that don't have trim().
Does this handle all Unicode whitespace?
The \s character class in JavaScript matches standard whitespace (space, tab, newline, carriage return, form feed, vertical tab). For Unicode whitespace characters (non-breaking space, etc.), use \p{White_Space} with the u flag.