将具有编码字符的字符串转换为可读字符串在Javascript中

huangapple go评论68阅读模式
英文:

Convert string with encoded character to readable string in Javascript

问题

Desired output: "Università di Parma"

英文:

An API is returning string with special characters in this form:
"Universit\xc3\xa0 di Parma"
\xc3\xa0 means the character "à"

How can I convert this string into a readable form with JavaScript code?
Thank you !

Input: "Universit\xc3\xa0 di Parma"

Desidered output: "Università di Parma"

答案1

得分: 1

Use the escape() method with decodeURIComponent.

const original = 'Università di Parma';
const result = decodeURIComponent(escape(original));
console.log(result);
英文:

Use the escape() method with decodeURIComponent.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const original = &#39;Universit\xc3\xa0 di Parma&#39;;
const result = decodeURIComponent(escape(original));
console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 00:27:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定