base64Copy

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

Created by: C15C01337
Installed 1 times

Category: Convert

Created on: Thursday, December 5, 2024 at 9:23:05 PM

Updated on: Wednesday, December 11, 2024 at 10:36:25 PM

Tag arguments
[]
Code
class base64Copy {
  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;
     }
  }

}