// 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, "" );
},
Analysis: The function of jquery trim() is to delete the spaces that appear on both sides of the string;
The key implementation is text.toString().replace( trimLeft, "" ).replace( trimRight, "" );
is to call replace twice on the incoming string. The regular expression trimLeft matches the space on the left and trimRight matches the space on the right.