Search code examples
javascriptajaxstringcharacter-encoding

Convert string with encoded character to readable string in Javascript


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"


Solution

  • Use the escape() method with decodeURIComponent.

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