Copy link to clipboard
Copied
Hi,
I'm having issues decoding a string ...
var newText = "This is a [u0027] test";
newText = newText.replace("\[u", '\\u');
newText = newText.replace("]", "");
newText = decodeURI(newText);
alert(newText);
The base string contains square brackets and lacks the backspace prefix.
This however seem to work ...
var newText = "This is a \u0027 test";
newText = decodeURI(newText);
alert(newText);
Does aynone have a solution?
Thanx
To get a string from a Unicode number you'd use
String.fromCharCode (0x0027);
You can't use this construct as the replacement value in a replace() call, but you can use a function as the replacement parameter:
var newText = "This is a [u0027] test";
newText = newText.replace (/\[u(....)\]/, function () {
return String.fromCharCode ('0x'+arguments[1]);
});
newText = decodeURI(newText);
alert(newText);
But in fact there is no need to decodeURI newText, you can delete than line.
P.
Copy link to clipboard
Copied
I think no need to escape [ in first replace? Or escape ] in second replace?
Copy link to clipboard
Copied
the replacement of the brackets works fine. But it doesn't seem to decode the result ...
Copy link to clipboard
Copied
To get a string from a Unicode number you'd use
String.fromCharCode (0x0027);
You can't use this construct as the replacement value in a replace() call, but you can use a function as the replacement parameter:
var newText = "This is a [u0027] test";
newText = newText.replace (/\[u(....)\]/, function () {
return String.fromCharCode ('0x'+arguments[1]);
});
newText = decodeURI(newText);
alert(newText);
But in fact there is no need to decodeURI newText, you can delete than line.
P.
Copy link to clipboard
Copied
Thanks Peter. Works perfectly!