Javascript string comparison fails when comparing unicode characters

后端 未结 4 1319
温柔的废话
温柔的废话 2020-11-30 08:18

I want to compare two strings in JavaScript that are the same, and yet the equality operator == returns false. One string contains a special character (eg. the

4条回答
  •  半阙折子戏
    2020-11-30 08:43

    Unlike what some other people here have said, this has nothing to do with encodings. Rather, your two strings use different code points to render the same visual characters.

    To solve this correctly, you need to perform Unicode normalization on the two strings before comparing them. Unforunately, JavaScript doesn't have this functionality built in. Here is a JavaScript library that can perform the normalization for you: https://github.com/walling/unorm

提交回复
热议问题