Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  168] [ 2]  / answers: 1 / hits: 63541  / 10 Years ago, wed, february 26, 2014, 12:00:00

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:


/^somes{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).


More From » jquery

 Answers
25

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

[#72302] Tuesday, February 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lindsay

Total Points: 402
Total Questions: 109
Total Answers: 109

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
;