Compare Strings With Different Encodings
I've just needed to compare to strings in JavaScript, and the comparision of specific strings failed sometimes. One value was obtained with jQuery via the text() method (from some
Solution 1:
So at the end it's the whitespace with different encodings which made my comparison fails.
No, it is not a different encoding. It is just a different whitespace - a non-breaking space.
Can I just replace the white space to be equal? But I guess there are other control characters - like tab, return and so on - which could mess up my comparison?
You can replace all of them. You might want to try something like
value1.replace(/\s+/g, " ").replace(/^\s*|\s$/g, "") == value2
which joins multiple whitespaces (of all kinds, including returns) to a single space and also trims the string before the comparison.
Post a Comment for "Compare Strings With Different Encodings"