I noticed that trim() does not remove new line characters from the start and end of a string, so I am trying to accomplish this with the following regex:
return
Try this:
str = str.replace(/^\s+|\s+$/g, '');
jsFiddle here.