Search code examples
javascriptencodingquoted-printable

Javascript decode =C3=B3 quoted printable


I have search a lot but cant find the solution, i want to decode a word like

Documentaci=C3=B3n => Documentación

in javascript, but i cant.

I tried to convert to utf8 when word is like

Documentaci%C3%B3n

and i can, but never with

that "=" instead of "%".

I take that text from an email if that helps.

Thanks in advance if you can help me, really.


Solution

  • Just replace that '=' with '%':

    var str = "Documentaci=C3=B3n";
    str = str.replace(/={1}/g, '%');
    str = decodeURI(str);
    

    It worked for me: http://jsfiddle.net/5bkpw5kw/1/

    UPD1: This is 'quoted-printable' encoding, as can be learned from raw letter. I googled and tried deal of functions, here some of them

    .. and others

    I also has taken code snippet from https://mothereff.in/quoted-printable (unbeautified script is embeded into html):

    var j = String.fromCharCode;
    var a = function(l) {
        return l.replace(/[\t\x20]$/gm, "").replace(/=?(?:\r\n?|\n)/g, "").replace(/=([a-fA-F0-9]{2})/g, function(n, m) {
            var o = parseInt(m, 16);
            return j(o)
        })
    };
    

    All of them return me Documentación , but sinse said snippet works fine on mothereff.in I assume all of them are valid. That must be some UTF-8 issue that I haven't still fixed. see this or google

    Updated fiddle: http://jsfiddle.net/5bkpw5kw/2/

    UPD2: working example by author http://jsfiddle.net/fyoc1bvk/2