ES2019 引入了 String.prototype.trimStart()
和 String.prototype.trimEnd()
const string = ' hello world ';
string.trimStart();
// → 'hello world '
string.trimEnd();
// → ' hello world'
string.trim(); // ES5
// → 'hello world'
此功能先前可透過非標準的 trimLeft()
和 trimRight()
方法取得,這些方法仍作為新方法的別名,以維持向後相容性。
const string = ' hello world ';
string.trimStart();
// → 'hello world '
string.trimLeft();
// → 'hello world '
string.trimEnd();
// → ' hello world'
string.trimRight();
// → ' hello world'
string.trim(); // ES5
// → 'hello world'
String.prototype.trim{Start,End}
支援 #
- Chrome: 自版本 66 起支援
- Firefox: 自版本 61 起支援
- Safari: 自版本 12 起支援
- Node.js: 自版本 8 起支援
- Babel: 支援