Trim

  • 版本网址缩略名: JavaScript/Reference/Global_Objects/String/Trim
  • 版本标题: Trim
  • 版本 id: 43426
  • 创建于:
  • 创建者: ziyunfei
  • 是否是当前版本?
  • 评论 1 words added, 1 words removed

修订内容

{{ js_minversion_header("1.8.1") }}

概述

删除字符串两端的空白字符.

Method of String
Implemented in JavaScript 1.8.1
ECMAScript Edition ECMAScript 5th Edition

语法

string.trim()

参数

None.

描述

trim 方法返回经过删除两端空白字符的字符串.trim方法并不影响原字符串本身.

例子

下面的例子显示小写字母 "foo":

var orig = "   foo  ";
alert(orig.trim());

相关链接

兼容性

如果浏览器本身不支持String对象的trim方法,那么运行下面的代码可以为浏览器添加 String.trim()

if(!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g,'');
  };
}

浏览器兼容性

基于 Kangax's compat tables

{{ CompatibilityTable() }}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} 9 10.5 5
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/Trim","en": "en/JavaScript/Reference/Global_Objects/String/Trim"} ) }}

修订版来源

<p>{{ js_minversion_header("1.8.1") }}</p>
<h2 id="Summary" name="Summary">概述</h2>
<p>删除字符串两端的空白字符.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/zh-cn/JavaScript/Reference/Global_Objects/String" title="zh-cn/JavaScript/Reference/Global_Objects/String"><code>String</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.8.1</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 5th Edition</td> </tr> </tbody>
</table>
<h2 id="Syntax" name="Syntax">语法</h2>
<p><code><em>string</em>.trim()</code></p>
<h2 id="Parameters" name="Parameters">参数</h2>
<p>None.</p>
<h2 id="Description" name="Description">描述</h2>
<p><code>trim</code> 方法返回经过删除两端空白字符的字符串.<code>trim方法</code>并不影响原字符串本身.</p><h2 id="Examples" name="Examples">例子</h2>
<p>下面的例子显示小写字母 "foo":</p>
<pre class="brush: js">var orig = "   foo  ";
alert(orig.trim());
</pre>
<h2 id="See_Also" name="See_Also">相关链接</h2>
<ul> <li><a class="internal" href="/zh-cn/JavaScript/Reference/Global_Objects/String/TrimLeft" title="zh-cn/Core JavaScript 1.5 Reference/Global Objects/String/trimLeft"><code>trimLeft()</code></a></li> <li><a class="internal" href="/zh-cn/JavaScript/Reference/Global_Objects/String/TrimRight" title="zh-cn/Core JavaScript 1.5 Reference/Global Objects/String/trimRight"><code>trimRight()</code></a></li>
</ul>
<h2 id="See_also" name="See_also">兼容性</h2>
<p>如果浏览器本身不支持String对象的trim方法,那么运行下面的代码可以为浏览器添加 <code>String.trim</code>()</p>
<pre class="brush: js">if(!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/g,'');
  };
}
</pre>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>基于 <a class="external" href="http://kangax.github.com/es5-compat-table/">Kangax's compat tables</a></p>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox (Gecko)</th> <th>Chrome</th> <th>Internet Explorer</th> <th>Opera</th> <th>Safari</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatVersionUnknown() }}</td> <td>{{ CompatVersionUnknown() }}</td> <td>9</td> <td>10.5</td> <td>5</td> </tr> </tbody> </table>
</div>
<div id="compat-mobile"> <table class="compat-table"> <tbody> <tr> <th>Feature</th> <th>Firefox Mobile (Gecko)</th> <th>Android</th> <th>IE Mobile</th> <th>Opera Mobile</th> <th>Safari Mobile</th> </tr> <tr> <td>Basic support</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> <td>{{ CompatUnknown() }}</td> </tr> </tbody> </table>
</div>
<p>{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/Trim","en": "en/JavaScript/Reference/Global_Objects/String/Trim"} ) }}</p>
恢复到这个版本