Is there any difference between jQuery trim and native JavaScript one?
Is there any other behaviour, safety, performance?
trim( str )Returns: Stringversion deprecated: 3.5. Description: Remove the whitespace from the beginning and end of a string.
The Trim method removes from the current string all leading and trailing white-space characters. Each leading and trailing trim operation stops when a non-white-space character is encountered. For example, if the current string is " abc xyz ", the Trim method returns "abc xyz".
Answer: Use the jQuery $. trim() function You can use the jQuery $. trim() function to remove all the spaces (including non-breaking spaces), newlines, and tabs from the beginning and end of the specified string.
The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
JavaScript .trim()
was defined in ES 5.1, and will not work for IE<9. Therefore, if you already use jQuery you could use the less performant $.trim()
jQuery's $.trim()
Method does:
trim: function( text ) {
return text == null ? "" : ( text + "" ).replace( rtrim, "" );
}
where rtrim
is basically this RegExp
new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" )
and whitespace
is represented as this Group:
"[\\x20\\t\\r\\n\\f]"
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.
So let alone the implementation of JS's .trim()
to browser vendors, the point is quite clear:
Remove any representation of newlines or spaces from the beginning and end of a String.
String.trim
The trim() method removes whitespace from both sides of a string.
Note: The trim() method does not change the original string.
var str = " Hello World! ";
alert(str.trim());
jquery.trim()
The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string.
If these whitespace characters occur in the middle of the string, they are preserved
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With