1

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.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
user3647848
  • 21
  • 1
  • 5

1 Answers1

6

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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
iVenGO
  • 384
  • 1
  • 4
  • 17
  • Yes, but cant do that in whole text, maybe there are correct "=", and to use decodeURI must be a string without spaces – user3647848 Sep 12 '14 at 21:10
  • Can you describe the situation - why it is urlencoded ? if this is part of url - there is no any = (%3D instead) nor spaces (+ or %20 instead). May be there is another error on the back-end or elswhere. – iVenGO Sep 12 '14 at 21:21
  • Hi iVenGO and thanks for the help, the text is from an email, if u use gmail and see the "original message" at the options, u can see there is this type of codification. i have search in google an email like that and u can see an example here: http://marc.info/?l=olpc-argentina&m=131707228310718 there is the codification at the subject – user3647848 Sep 12 '14 at 21:27
  • May be use RexExp 'lookahead' lookaround like so: (?=[A-Z0-9]{2}) – iVenGO Sep 12 '14 at 21:28
  • I can do it with regex, but that was my last option, i think must be a better solution maybe. like utf8_decode or something like that, but nothing works – user3647848 Sep 12 '14 at 21:34
  • 2 hours spent for your issue: 'asked 2 hours ago' :) – iVenGO Sep 12 '14 at 23:03
  • Really really thank you, finally that works. A working example is just here: http://jsfiddle.net/fyoc1bvk/ really thank you – user3647848 Sep 12 '14 at 23:53
  • welcome. append '(quoted-printable)' to your question if possible, for better searchablity – iVenGO Sep 13 '14 at 09:47
  • are you from Argentina? – iVenGO Sep 13 '14 at 10:00
  • No, im from Spain, the email was an example i found on Internet – user3647848 Sep 13 '14 at 10:05