String comparison of Japanese characters

I need two compare to strings to see if they are the same value, both strings are in Japanese. This check seems to fail and I assume it's some shortcoming of JavaScript and UTF-8. Should it be expected for this to succeed? And how can I make it work? Can JavaScript convert to strings like \x{57fc} to perform comparison on?

var foo = '郵便番号',
    bar = '郵便番号';

if(foo == bar){
    console.log('they are the same');
}

UPDATE

It appears this actually works just fine, the failure is a bigger issue with backbone validation. Thanks for the answers.

Answers


You forgot the quotes on your strings!

Run your current code with the browser console open, and you'll see a ReferenceError due to the missing quotes (the interpreter will think 郵便番号 is an identifier, not a string).

Wrap your strings in proper quotes, and it just works, JavaScript can handle UTF-8 just fine:

var foo = '郵便番号',
    bar = '郵便番号';

if(foo == bar){
    console.log('they are the same');
}

Regarding your second question, you can use '\u57fc', but usually you don't need to; it's the same as a literal '埼'.


Need Your Help

c++ for unix command line operation

c++ shell unix

I have a remote unix shell which I log on often to checkout files with but the system keep resetting my locals setting when I logon to it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.