trim() 方法會從一個字符串的兩端刪除空白字符。在這個上下文中的空白字符是所有的空白字符 (space, tab, no-break space 等) 以及所有行終止符字符(如 LF,CR)。
語法:

trim()方法並不影響原字符串本身,它返回的是一個新的字符串。
用法
var orig = ' foo '; console.log(orig.trim(), orig.trim().length); // 'foo',3 // 另一個.trim()例子,只從一邊刪除 var orig = 'foo '; console.log(orig.trim(), orig.trim().length); // 'foo',3
兼容舊環境
/* \s:空格 \uFEFF:字節次序標記字符(Byte Order Mark),也就是BOM,它是es5新增的空白符 \xA0:禁止自動換行空白符,相當於html中的 */ if (!String.prototype.trim) { String.prototype.trim = function(){ return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,''); } }
