修剪空白

要從字串的邊緣修剪空白,請使用 String.prototype.trim

"    some whitespaced string  ".trim();  // "some whitespaced string"

許多 JavaScript 引擎,但不是 Internet Explorer ,已經實現了非標準的 trimLefttrimRight 方法。對於標準化的 trimStarttrimEnd 方法,目前處於該過程的第 1 階段的提議 ,為了相容性而別名為 trimLefttrimRight

// 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"