base64

This tag encodes and decodes base64 using browser APIs atob and btoa.

Created by: hackvertor
Installed 12 times

Category: Convert

Created on: Thursday, September 12, 2024 at 5:32:16 PM

Updated on: Wednesday, November 20, 2024 at 5:32:24 PM

This is a built in tag
Tag arguments
[]
Code
class base64 {
  encode(input) {
    return btoa(input);
  }

  decode(input) {
    return atob(input);
  }

  matches(input) {
     const base64Regex = /^(?:[A-Za-z0-9+/]{8,}={0,2}$)|[A-Za-z0-9+/]{8,}={0,2}/;
     const result = base64Regex.exec(input);
     if(result && result[0].length % 4 === 0) {
         return result;
     } else {
        return null;
     }
  }

}