Trim

  • Tags
  • Files

在 JavaScript 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

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support (Yes) (Yes) 9 10.5 5
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support ? ? ? ? ?

Contributors to this page: ziyunfei
Last updated by: ziyunfei,