Using standard Base64 in URLs requires encoding of +
, /
and =
characters into special percent-encoded hexadecimal sequences (+
becomes %2B
, /
becomes %2F
and =
becomes %3D
), which makes the string unnecessarily longer.
For this reason, modified Base64 for URL variants exist, where the +
and /
characters of standard Base64 are respectively replaced by -
and _
, so that using URL encoders/decoders are no longer necessary and have no impact on the length of the encoded value
Both above solutions lack a very usefull property for URLs. Double Click mouse selection!
Escape64 is a variable length variant of Base64 that solves this in a intiutive way.
_
is used as an escape character and we define the following escape sequences
__ beginging of an Escape64 sequence (optional)
_p encodes a (+) of Base64
_d encodes a (/) of Base64
_q encodes a (=) pad character of Base64
_b marks breaks inside an Escape64 sequence (optional)
_* Reserved. Where (*) can be any char not expicitly mentioned above.
var x = 'asdf+ff/f+ff/dd+ddAd=='; //base64 value
var y = x.replace(/[+]/g,'_p').replace(/[\/]/g,'_d').replace(/[=]/g,'_q');//escape64 value encode
var z = y.replace(/_p/g,'+').replace(/_d/g,'/').replace(/_q/g,'='); //escape64 value decode
console.log(x, y, z);
console.log(z);
console.log(x==z);
npm install escape64
See example.js