Skip to content

Instantly share code, notes, and snippets.

@geraintluff
Created October 9, 2015 15:36
Show Gist options
  • Save geraintluff/21beb1066fc5239304aa to your computer and use it in GitHub Desktop.
Save geraintluff/21beb1066fc5239304aa to your computer and use it in GitHub Desktop.
Convert base64 to and from web-safe variant
// Convert from normal to web-safe, strip trailing "="s
function webSafe64(base64) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
// Convert from web-safe to normal, add trailing "="s
function normal64(base64) {
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4);
}
@Booligoosh
Copy link

Thanks so much! Super helpful <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment