Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery.trim compared to string.trim

Is there any difference between jQuery trim and native JavaScript one?

Is there any other behaviour, safety, performance?

like image 802
oneat Avatar asked Oct 30 '16 12:10

oneat


People also ask

Is trim deprecated?

trim( str )Returns: Stringversion deprecated: 3.5. Description: Remove the whitespace from the beginning and end of a string.

What trim () in string does?

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".

How do I remove spaces between words in jQuery?

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.

How do I use string prototype trim?

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.).


2 Answers

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]"


In JavaScript (ES6) it's defined as:

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.

like image 77
Roko C. Buljan Avatar answered Oct 15 '22 22:10

Roko C. Buljan


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

like image 42
CodeMan Avatar answered Oct 15 '22 23:10

CodeMan