Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the JavaScript String whitespace character   not match?

Tags:

I got in HTML the following construct:

<div id="text">   some&nbsp;text </div> 

If I trim the text and test it with:

$("#text").text().trim() === "some text" 

it returns false, also:

$("#text").text().trim() === "some&nbsp;text" 

returns false, but:

/^some\s{1}text$/.test($("#text").text().trim()) 

returns true. So please tell me, what´s wrong here.

As you would suggest, I am using jQuery (1.6).

like image 598
alpham8 Avatar asked Feb 26 '14 09:02

alpham8


People also ask

What is whitespace characters in JavaScript?

Whitespace refers to characters which are used to provide horizontal or vertical space between other characters. Whitespace is often used to separate tokens in HTML, CSS, JavaScript, and other computer languages.

Is whitespace string JavaScript?

Description. In JavaScript, trim() is a string method that is used to remove whitespace characters from the start and end of a string. Whitespace characters include spaces, tabs, etc.

Does JavaScript care about whitespace?

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript programs. You can use spaces, tabs, and newlines freely in your program and you are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.

What is the purpose of whitespace characters?

Space, tab, line feed (newline), carriage return, form feed, and vertical tab characters are called "white-space characters" because they serve the same purpose as the spaces between words and lines on a printed page — they make reading easier.


2 Answers

That's because the no breaking space (charCode 160) does not exactly equal to space (charCode 32)

jquery's .text() encodes HTML entities to their direct unicode equivalence, and so &nbsp; becomes String.fromCharCode(160)

You can solve it by replaceing all the the non-breaking spaces with ordinary spaces:

d.text().replace(String.fromCharCode(160) /* no breaking space*/,          " " /* ordinary space */) == "some text" 

or better yet:

d.text().replace(/\s/g /* all kinds of spaces*/,          " " /* ordinary space */) == "some text" 
like image 140
TastySpaceApple Avatar answered Oct 09 '22 12:10

TastySpaceApple


&nbsp; is not the same as the space character (Unicode U+0020). It's a non-breaking space character, encoded in Unicode as U+00A0. This is why the first of your tests doesn't match, but the third one does; \s matches all white space characters.

Either stick to your regular expression test, or use \u00a0 or \xa0 in your equality check:

$("#text").text().trim() === "some\xa0text"; $("#text").text().trim() === "some\u00a0text"; 
like image 26
Andy E Avatar answered Oct 09 '22 12:10

Andy E