<span style="font-size:18px;">
var str=" word ";
//消除左边的空格
alert("hello"+str);
alert( "hello"+str.replace(/^\s*/g,""));
//消除右边的空格
alert("hello"+str+"tom");
alert( "hello"+str.replace(/\s*$/g,"")+"tom");
//消除前后的空格
alert("hello"+str+"tom");
alert( "hello"+str.replace(/(^\s*)|(\s*$)/g,"")+"tom");
</span>