Suppose I have a string created like this: str = '\\' + 'u00eb'
. If I do document.createTextNode(str)
, it will give me '\u00eb
', instead of ë
. Dont't ask me why, but I can't define my string as str = '\u00eb'
. Defining my string as str = 'ë'
or 'ë'
doesn't help me either. Any suggestions on how to get 'ë'
printed?
Asked
Active
Viewed 988 times
3

Spifff
- 748
- 6
- 14
-
2Why can't you define it as `str = '\u00eb'`? – Musa Sep 08 '13 at 16:35
-
One possibility is `var s = eval('"\\' + 'u00eb"');`. I know you said to not ask why, but I am curious why you can't use the proper escape sequence directly in your code – user2736012 Sep 08 '13 at 16:36
-
`str = '\' + 'u00eb'` is a syntax error. – Pointy Sep 08 '13 at 16:40
-
I cannot write '\u00eb' directly, since I get the string from an XML file using XMLHttpRequest, over which I have limited control. – Spifff Sep 08 '13 at 16:40
-
@PieterN then you're going to have to interpret the numeric value with your own code and then use `String.fromCharCode()` to build the string. – Pointy Sep 08 '13 at 16:40
-
You're right about the syntax error, it should be '\\' + 'u00eb'! – Spifff Sep 08 '13 at 16:41
-
@PieterN: Thanks. So the XML just returns the `u00eb` part. Perhaps better than `eval` would be `var s = new Function('return "\\' + 'u00eb' + '";')();` – user2736012 Sep 08 '13 at 16:42
-
I'll try both the eval and the new Function methods. – Spifff Sep 08 '13 at 16:44
-
My strings can be something like 'This is a tëst', I have them converted now using something like str = eval('"' + str + '"'). I just have to be sure '"' is not part of the original str. – Spifff Sep 08 '13 at 16:50
-
I don't understand. If that's your string, it already has the proper unicode character, so there should be no need for an escape sequence. – user2736012 Sep 08 '13 at 16:52
-
I should be more specific: my string 'This is a tëst' will be received as 'This is a t\u00ebst'. – Spifff Sep 08 '13 at 16:55
-
@PieterN: Alright, then the solutions I gave above should work. If you want something a little safer, then you could use `JSON.parse()` instead. `var s = JSON.parse('"' + 'This is a t\u00ebst' + '"');` – user2736012 Sep 08 '13 at 17:52
2 Answers
1
How about
var str = String.fromCharCode(0xeb);

Pointy
- 405,095
- 59
- 585
- 614
-
That should work, if it would always be 0xeb that I want to translate. Unfortunately, str can contain all kinds of characters... Sorry about my unclear questioning – Spifff Sep 08 '13 at 16:43
-
@PieterN well as I said in the comment to your question, you'll have to write code to detect character code values in your XML, interpret them as numeric values, and then construct the string. You have not provided a sample of what your XML actually looks like, so it's hard to be more specific. – Pointy Sep 08 '13 at 16:45
1
If the data coming in looks like this:
u00eb
then, you can parse the hex string yourself:
function insertText(parent, str) {
if (str.charAt(0) == 'u') {
str = String.fromCharCode(parseInt(str.slice(1), 16));
}
parent.appendChild(document.createTextNode(str));
}
Working demo: http://jsfiddle.net/jfriend00/pk5Bp/

jfriend00
- 683,504
- 96
- 985
- 979