Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Issue decoding a string

Contributor ,
Jun 28, 2023 Jun 28, 2023

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

 

TOPICS
Scripting
524
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 28, 2023 Jun 28, 2023

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.

Translate
Community Expert ,
Jun 28, 2023 Jun 28, 2023

I think no need to escape [ in first replace? Or escape ] in second replace?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 28, 2023 Jun 28, 2023

the replacement of the brackets works fine. But it doesn't seem to decode the result ...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2023 Jun 28, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 28, 2023 Jun 28, 2023
LATEST

Thanks Peter. Works perfectly!

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines