JSFiddle - React, Tailwind, and code Playground
by Estevez Ben Lajamin
JavaScript
isBase64("U2FsdGVkX19aJZoKvZpHvW+bXhzDGH8eYzKb+O7rp2COEL2Q8vtavAwVenYaVUBN");
const notBase64 = /[^A-Z0-9+\/=]/i;
export default function isBase64(str) {
assertString(str); // remove this line and make sure you pass in a string
const len = str.length;
if (!len || len % 4 !== 0 || notBase64.test(str)) {
return false;
}
const firstPaddingChar = str.indexOf('=');
return firstPaddingChar === -1 ||
firstPaddingChar === len - 1 ||
(firstPaddingChar === len - 2 && str[len - 1] === '=');
}