jQuery源码分析7: jQuery.trim

jQuery源码分析7: jQuery.trim

// Used for trimming whitespace
var trimLeft = /^\s+/,
trimRight = /\s+$/,
trim = String.prototype.trim; //< JavaScript直到1.8.1才支持trim

// Check if a string has a non-whitespace character in it
rnotwhite = /\S/,

//< 在ie中不换行符号non-break('\xA0')不算作空白,即不在字符集[\s]中,但在其他浏览器则属于空白,所以在jQuery中针对ie作了加强处理
if ( rnotwhite.test( "\xA0" ) ) {
trimLeft = /^[\s\xA0]+/;
trimRight = /[\s\xA0]+$/;
}

// < 若支持原生的String.prototype.tim则直接使用其过滤字符串两侧的空白,否则使用replace过滤两侧的空白
trim: trim ?
function( text ) {
return text == null ?
"" :
trim.call( text );
} :

// Otherwise use our own trimming functionality
function( text ) {
return text == null ?
"" :
text.toString().replace( trimLeft, "" ).replace( trimRight, "" );

},


补充

最后需要提到的是ECMA-262(V5)中为String提供原生的trim方法.此外Molliza Gecko 1.9.1引擎中还给String添加了trimLeft,trimRight方法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值