trimStart() method removes whitespace from the beginning of a string. trimLeft() is an alias of this method.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

str.trimStart();
str.trimLeft();
					

返回值

A new string representing the calling string stripped of whitespace from its beginning (left end).

描述

trimStart() / trimLeft() methods return the string stripped of whitespace from its left end. trimLeft() or trimStart() do not affect the value of the string itself.

Aliasing

For consistency with functions like String.prototype.padStart the standard method name is trimStart . However, for web compatibility reasons, trimLeft remains as an alias to trimStart . In some engines this means:

String.prototype.trimLeft.name === "trimStart";
					

Polyfill

//https://github.com/FabioVergani/js-Polyfill_String-trimStart
(function(w){
    var String=w.String, Proto=String.prototype;
    (function(o,p){
        if(p in o?o[p]?false:true:true){
            var r=/^\s+/;
            o[p]=o.trimLeft||function(){
                return this.replace(r,'')
            }
        }
    })(Proto,'trimStart');
})(window);
/*
ES6:
(w=>{
    const String=w.String, Proto=String.prototype;
    ((o,p)=>{
        if(p in o?o[p]?false:true:true){
            const r=/^\s+/;
            o[p]=o.trimLeft||function(){
                return this.replace(r,'')
            }
        }
    })(Proto,'trimStart');
})(window);
*/
					

范例

使用 trimStart()

The following example displays the lowercase string 'foo  ' :

var str = '   foo  ';
console.log(str.length); // 8
str = str.trimStart();
console.log(str.length); // 5
console.log(str);        // 'foo  '
					

规范

规范
ECMAScript (ECMA-262)
The definition of ' String.prototype.trimStart' in that specification.

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
trimStart Chrome 66
66
4 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
Edge 12 Alternate Name
12 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
Firefox 61
61
3.5 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
IE No Opera 53
53
15 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
Safari 12 WebView Android 66
66
≤37 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
Chrome Android 66
66
18 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
Firefox Android 61
61
4 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
Opera Android 47
47
14 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
Safari iOS 12 Samsung Internet Android 9.0
9.0
1.0 Alternate Name
Alternate Name Uses the non-standard name: trimLeft
nodejs 10.0.0
10.0.0
0.12 Alternate Name
Alternate Name Uses the non-standard name: trimLeft

图例

完整支持

完整支持

不支持

不支持

使用非标名称。

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. String
  3. 特性
    1. String 长度
  4. 方法
    1. String.fromCharCode()
    2. String.fromCodePoint()
    3. String.prototype.anchor()
    4. String.prototype.big()
    5. String.prototype.blink()
    6. String.prototype.bold()
    7. String.prototype.charAt()
    8. String.prototype.charCodeAt()
    9. String.prototype.codePointAt()
    10. String.prototype.concat()
    11. String.prototype.endsWith()
    12. String.prototype.fixed()
    13. String.prototype.fontcolor()
    14. String.prototype.fontsize()
    15. String.prototype.includes()
    16. String.prototype.indexOf()
    17. String.prototype.italics()
    18. String.prototype.lastIndexOf()
    19. String.prototype.link()
    20. String.prototype.localeCompare()
    21. String.prototype.match()
    22. String.prototype.matchAll()
    23. String.prototype.normalize()
    24. String.prototype.padEnd()
    25. String.prototype.padStart()
    26. String.prototype.repeat()
    27. String.prototype.replace()
    28. String.prototype.replaceAll()
    29. String.prototype.search()
    30. String.prototype.slice()
    31. String.prototype.small()
    32. String.prototype.split()
    33. String.prototype.startsWith()
    34. String.prototype.strike()
    35. String.prototype.sub()
    36. String.prototype.substr()
    37. String.prototype.substring()
    38. String.prototype.sup()
    39. String.prototype.toLocaleLowerCase()
    40. String.prototype.toLocaleUpperCase()
    41. String.prototype.toLowerCase()
    42. String.prototype.toSource()
    43. String.prototype.toString()
    44. String.prototype.toUpperCase()
    45. String.prototype.trim()
    46. String.prototype.trimEnd()
    47. String.prototype.trimStart()
    48. String.prototype.valueOf()
    49. String.prototype[@@iterator]()
    50. String.raw()
  5. 继承:
  6. Function
  7. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  9. Object
  10. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  11. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1