trimLeft = /^[\s\xA0]+/;
trimRight = /[\s\xA0]+$/;
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()函数的实现原理,通过正则表达式trimLeft和trimRight去除字符串首尾空白字符,介绍了如何在JavaScript中实现字符串的高效修剪。
242

被折叠的 条评论
为什么被折叠?



