Why does the JavaScript String whitespace character not match?
javascript, jquery, regex
Solution
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"
Problem
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).