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

Issue decoding a string

Contributor ,
Jun 28, 2023 Jun 28, 2023

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

 

TOPICS
Scripting

Views

264

Translate

Translate

Report

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.

Votes

Translate

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

Thanks Peter. Works perfectly!

 

Votes

Translate

Translate

Report

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