Base64 String in JS
Match Base64-encoded strings, including proper padding with = and == characters.
Try it in the JS tester →Pattern
regexJS
(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)? (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=)?", "g");
const input = "SGVsbG8gV29ybGQ=";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
The pattern matches groups of 4 Base64 characters ((?:[A-Za-z0-9+\/]{4})*) then allows the trailing partial group: 3 chars + one =, or 2 chars + ==. This enforces valid Base64 padding rules.
Examples
Input
SGVsbG8gV29ybGQ=Matches
SGVsbG8gV29ybGQ=
Input
dGVzdA==Matches
dGVzdA==
Input
not!base64!@No match
—