我是靠谱客的博主 冷艳月饼,这篇文章主要介绍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是匹配右边的空格

最后

以上就是冷艳月饼最近收集整理的关于jquery trim() 功能源代码的全部内容,更多相关jquery内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(133)

评论列表共有 0 条评论

立即
投稿
返回
顶部