Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
ECMAScript (JavaScript)

/
(^\s+|\s+$)
/
gm

Description

Remove whitespace before and after the string. Use g for trimming before and after Use m for trimming each line of a multiline string individually.

Code: ' test '.replace( /(^\s+|\s+$)/gm, '' );

Submitted by KevinV - 9 years ago