Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery trim doesn't remove  ?

How can I trim all spaces from a string, even when they are caused by a non-breaking spaces ( )

For example:

var foo = $.trim($('<p>&nbsp;foo&nbsp;</p>').text());

The value of foo is " foo " instead of "foo"

UPDATE So, the problem wasn't jQuery's trim function. It works great. The problem is MSAjax's trim function. jQuery, rightly, uses function detection and if they don't exist, uses it's implementation.

Unfortunately, MSAJax's implementation of trim doesn't strip char 160 (non breaking space). However, the jQuery regex trim does, as it realizes that IE doesn't include char 160 in \s.

Why do stupid browser problems always end up being an issue with how M$ implements something???

like image 463
CaffGeek Avatar asked Sep 14 '11 15:09

CaffGeek


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.

How do I remove whitespace from start and end of string?

String result = str. trim(); The trim() method will remove both leading and trailing whitespace from a string and return the result.

How do you remove the space in the middle of a string?

Method 1: Using split() and join() Method: The split() method is used to split a string into multiple sub-strings and return them in the form of an array.


2 Answers

So, the problem wasn't jQuery's trim function. It works great. The problem is MSAjax's trim function. jQuery, rightly, uses function detection and if they don't exist, uses it's implementation.

Unfortunately, MSAJax's implementation of trim doesn't strip char 160 (non breaking space). However, the jQuery regex trim does, as it realizes that IE doesn't include char 160 in \s.

Why do stupid browser problems always end up being an issue with how M$ implements something???

like image 134
CaffGeek Avatar answered Oct 12 '22 07:10

CaffGeek


Are you sure about that? I tried a slightly modified version of your snippet just now:

var foo = $.trim($('<p>&nbsp;foo&nbsp;</p>').text());
alert('#' + foo + '#');

and there were no spaces on either side of the 'foo'. http://jsfiddle.net/Ux7Wc/

This was using jQuery 1.6.2 in Firefox 5.

like image 24
Dan Avatar answered Oct 12 '22 08:10

Dan