gpt4 book ai didi

javascript - jquery.trim 与 string.trim 相比

转载 作者:数据小太阳 更新时间:2023-10-29 03:52:45 26 4
gpt4 key购买 nike

jQuery trim 和原生 JavaScript trim 之间有什么区别吗?

是否有其他行为、安全、性能?

最佳答案

JavaScript .trim() 是在 ES 5.1 中定义的,不适用于 IE<9。因此,如果您已经在使用 jQuery,则可以使用性能较低的 $.trim()


jQuery 的 $.trim() 方法:

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

rtrim 基本上就是这个 RegExp

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

whitespace表示为这个组:

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


JavaScript (ES6)它被定义为:

21.1.3.25 String.prototype.trim ( )

This function interprets a String value as a sequence of UTF-16 encoded code points, as described in 6.1.4.

The following steps are taken: Let O be RequireObjectCoercible(this value). Let S be ToString(O). ReturnIfAbrupt(S). Let T be a String value that is a copy of S with both leading and trailing white space removed. The definition of white space is the union of WhiteSpace and LineTerminator. When determining whether a Unicode code point is in Unicode general category “Zs”, code unit sequences are interpreted as UTF-16 encoded code point sequences as specified in 6.1.4. Return T. NOTE The trim function is intentionally generic; it does not require that its this value be a String object. Therefore, it can be transferred to other kinds of objects for use as a method.

所以先不说JS的.trim()对浏览器厂商的实现,重点很明确:
从字符串的开头和结尾删除换行符或空格的任何表示。

关于javascript - jquery.trim 与 string.trim 相比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com