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, '' );