aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/trimstart/index.html
blob: 48718fcb13d64bb85b3f2a3b8a3598bea32c1000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
---
title: String.prototype.trimStart()
slug: Web/JavaScript/Reference/Global_Objects/String/trimStart
tags:
  - JavaScript
  - Method
  - Prototype
  - String
  - 参考
  - 字符串
  - 方法
translation_of: Web/JavaScript/Reference/Global_Objects/String/trimStart
original_slug: Web/JavaScript/Reference/Global_Objects/String/TrimLeft
---
<div>{{JSRef}}</div>

<div><strong><code>trimStart()</code> </strong>方法从字符串的开头删除空格。<code>trimLeft()</code> 是此方法的别名。</div>

<div>{{EmbedInteractiveExample("pages/js/string-trimstart.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox"><var>str</var>.trimStart();
<var>str</var>.trimLeft();</pre>

<h3 id="返回值">返回值</h3>

<p>一个新字符串,表示从其开头(左端)除去空格的调用字符串。</p>

<h2 id="描述">描述</h2>

<p><code>trimStart()</code> / <code>trimLeft()</code> 方法移除原字符串左端的连续空白符并返回一个新字符串,并不会直接修改原字符串本身。</p>

<h3 id="别名">别名</h3>

<p>为了与 {{jsxref("String.prototype.padStart")}} 等函数保持一致,标准方法名称为<code>trimStart</code>。 但是,出于 Web 兼容性原因,<code>trimLeft</code> 仍然是 <code>trimStart</code> 的别名。在某些引擎中,这意味着:</p>

<pre class="brush: js">String.prototype.trimLeft.name === "trimStart";</pre>

<h2 id="示例">示例</h2>

<h3 id="使用_trimStart">使用 <code>trimStart()</code></h3>

<p>下面的例子输出了小写的字符串 <code>"foo  "</code></p>

<pre class="brush:js;highlight:[5]">var str = "   foo  ";

console.log(str.length); // 8

str = str.trimStart()    // 等同于 str = str.trimLeft();
console.log(str.length); // 5
console.log(str);        // "foo  "
</pre>

<h2 id="规范">规范</h2>

<table>
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code><a href="https://github.com/tc39/proposal-string-left-right-trim/#stringprototypetrimstart--stringprototypetrimend">String.prototype.{trimStart,trimEnd}</a></code>proposal</td>
   <td>Stage 4</td>
   <td>Expected to be part of ES2019</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("javascript.builtins.String.trimStart")}}</p>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">// 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=&gt;{
    const String=w.String, Proto=String.prototype;

    ((o,p)=&gt;{
        if(p in o?o[p]?false:true:true){
            const r=/^\s+/;
            o[p]=o.trimLeft||function(){
                return this.replace(r,'')
            }
        }
    })(Proto,'trimStart');

})(window);
*/</pre>

<h2 id="参见">参见</h2>

<ul>
 <li>{{jsxref("String.prototype.trim()")}}</li>
 <li>{{jsxref("String.prototype.trimEnd()")}}</li>
</ul>