Do this in the console:
" foo ".replace(/^\s+|\s+$/g,'')