I got in HTML the following construct:
<div id="text"> some text </div>
If I trim the text and test it with:
$("#text").text().trim() === "some text"
it returns false
, also:
$("#text").text().trim() === "some 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).
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.
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.
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.
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.
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
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"
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With