I just can't understand it, but here is my situation.
I have this peace of code:
someFunction: function(content){
content = content.substr(19005,24);
console.log('content is: '+content);
content = decodeURIComponent(content);
console.log(typeof content, content);
var string = '\u0430\u0437\u0443\u0439';
string = decodeURIComponent(string);
console.log(typeof string, string);
}
And when I run this on my node.js server it returns this "abnormal" result:
content is: \u0430\u0437\u0443\u0439
string \u0430\u0437\u0443\u0439 // but should be "string азуй" as below
string азуй
So, how is it actually possible??
1) The same content
2) The same variable type
3) The same (decodeURIComponent) function
P.S. The only difference I see is in origin of content
and string
vars. But is this play a role?
The second string you created is not a string of characters with backslashes inside it. Rather, it's a string of unicode characters. When creating a string in javascript you can escape with the backslash and give a unicode character number. This allows for special characters that are outside the normal type-able keys. (Not completely accurate but you get the idea).
To get this to work you'd need to do this:
var string = '\\u0430\\u0437\\u0443\\u0439';
This double escape means you actually have backslashes instead of an escape sequence.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#String_literals for more details.
Edit: It sounds like you're asking how to go from the first string to the actual unicode characters. Use this (answer take from How do I decode a string with escaped unicode?):
var content = content.substr(19005,24);
var r = /\\u([\d\w]{4})/gi;
content = content.replace(r, function (match, grp) {
return String.fromCharCode(parseInt(grp, 16)); } );
content = unescape(content);