现在的位置: 首页 > 综合 > 正文

jquery trim() 功能源代码

2018年05月22日 ⁄ 综合 ⁄ 共 532字 ⁄ 字号 评论关闭
jquery trim() 作用是,删除字符串两边出现的空格

// Used for trimming whitespace
trimLeft = /^\s+/,
trimRight = /\s+$/,

// Use native String.trim function wherever possible
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, "" );
},

分析:jquery trim() 作用是,删除字符串两边出现的空格;

其中的关键实现是text.toString().replace( trimLeft, "" ).replace( trimRight, "" );

是将传入的字符串分别两次调用replace,其中正则表达trimLeft是匹配左边的空格,trimRight是匹配右边的空格

http://www.jb51.net/article/26255.htm

抱歉!评论已关闭.