Home  >  Article  >  Web Front-end  >  jquery trim() function source code_jquery

jquery trim() function source code_jquery

WBOY
WBOYOriginal
2016-05-16 18:10:51814browse
Copy code The code is as follows:

// 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.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn