You might want to take a look at jQuery.trim()
utilitiy:
Description:
Remove the whitespace from the beginning and end of a string. The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.
Example:
Remove the white spaces at the start and at the end of the string:
$.trim(" hello, how are you? ");
Result:
"hello, how are you?"