skip to content
Alvin Lucillo

Trim leading & trailing spaces

/ 1 min read

💻 Tech

To trim leading and trailing whitespaces, we can use a combination of exact match ^...$ and matching groups with ().

Regex: ^\s*(.*)\s*$ This ensures we match with zero or more whitespaces, zero or more characters, then zero or more whitespaces. We’re capturing the charactes between the whitespaces.

			The quick brown fox... -- captures 'The quick brown fox...'
	   jumps over the lazy dog.    -- captures 'jumps over the lazy dog.'