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

jquery trim() function source code_jquery

WBOY
Release: 2016-05-16 18:10:51
Original
830 people have browsed it
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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template