Jquery.trim compared to string.trim

Is there any difference between jQuery cropping and inline JavaScript?

Is there any other behavior, security, performance?

+4
source share
2 answers

JavaScript .trim()was defined in ES 5.1 and will not work for IE <9 . Therefore, if you are already using jQuery, you can use a less efficient$.trim()


jQuery $.trim() Method does:

trim: function( text ) {
    return text == null ? "" : ( text + "" ).replace( rtrim, "" );
}

where rtrimbasically is RegExp

new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" )

and whitespaceis represented as this group:

"[\\x20\\t\\r\\n\\f]"


In JavaScript (ES6), it is defined as:

21.1.3.25 String.prototype.trim ()

String UTF-16, 6.1.4.

: O - ( ). S - ToString (O). ReturnIfAbrupt (S). T - String, S , . - WhiteSpace LineTerminator. , Unicode "Zs", UTF-16, 6.1.4. . ; , String. .

JS .trim() , :
.

+4

String.trim

trim() .

. trim() .

var str = "       Hello World!        ";
alert(str.trim());

jquery.trim()

$.trim() , ( ) .

,

+1

Source: https://habr.com/ru/post/1659324/


All Articles