base64Copy

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

Created by: yuzadef
Installed 1 times

Viewed: 8

Category: Convert

Created on: Friday, June 6, 2025 at 7:47:43 AM

Updated on: Friday, June 6, 2025 at 7:47:43 AM

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;
    }
  }
}