Why does the JavaScript String whitespace character   not match?

I got in HTML the following construct:

<div id="text">

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:


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

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

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"

&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";

It is not taking into consideration the \n that is invisible.

Get rid of the \n and check it with ==.

Try this:

var x = $("#text").html();
x = x.replace(/(\r\n|\n|\r)/gm, "");
x = x.replace(/\s+/g, '');

if (x == 'some&nbsp;text') {
else {

Hope this helps.

There are a couple of similar questions. You could check them out.

Read More:   Display Bootstrap Modal using javascript onClick

Matching jquery .text() to &nbsp;
nonbreaking space

As mentioned in the previous posts. You could compare with “==” instead of “===” and use ‘\xa0’ for comparing &nbsp; which is the actual character for non-breaking space.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts