Regular Expressions 101

Trim Whitespace before/after String

Open Workspace
  • JavaScript
  • Substitution
1

Regular Expression

/
(^\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