还有其他行为,安全,性能吗?
解决方法
trim: function( text ) { return text == null ? "" : ( text + "" ).replace( rtrim,"" ); }
其中rtrim基本上就是这个RegExp
new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$","g" )
和空格表示为此组:
"[\\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()实现给浏览器供应商了,重点很明确:从String的开头和结尾删除任何换行符或空格的表示.