Regular Expressions Matching leading/trailing whitespace


Example

Trailing spaces

\s*$: This will match any (*) whitespace (\s) at the end ($) of the text

Leading spaces

^\s*: This will match any (*) whitespace (\s) at the beginning (^) of the text

Remarks

\s is a common metacharacter for several RegExp engines, and is meant to capture whitespace characters (spaces, newlines and tabs for example). Note: it probably won't capture all the unicode space characters. Check your engines documentation to be sure about this.