Javascript string comparison fails when comparing unicode characters

后端 未结 4 1313
温柔的废话
温柔的废话 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:34

    I had this same problem.

    Adding

    
    

    to the HTML file fixed the issue.

    In my case the templating engine was baking a json string into the HTML file. This string was in unicode.

    While the template was also a unicode file, the JS engine was treating the string I wrote into the template as a latin-1 encoded string, until I added the meta tag.

    I was comparing the typed in string to one of the JSON objects items (location.title == "Mühle")

提交回复
热议问题