修剪空白
要从字符串的边缘修剪空白,请使用 String.prototype.trim
:
" some whitespaced string ".trim(); // "some whitespaced string"
许多 JavaScript 引擎,但不是 Internet Explorer ,已经实现了非标准的 trimLeft
和 trimRight
方法。对于标准化的 trimStart
和 trimEnd
方法,目前处于该过程的第 1 阶段的提议 ,为了兼容性而别名为 trimLeft
和 trimRight
。
// Stage 1 proposal
" this is me ".trimStart(); // "this is me "
" this is me ".trimEnd(); // " this is me"
// Non-standard methods, but currently implemented by most engines
" this is me ".trimLeft(); // "this is me "
" this is me ".trimRight(); // " this is me"