Trimming Whitespace with Regular Expressions

Trailing white space:
[ \t]+$

Leading white space:
^[ \t]+

Both leading and trailing:
^[ \t]+|[ \t]+$

Also use the following if you want to trim line breaks too:
[ \t\r\n]

Leave a Reply