Skip to main content
Data & Formatting · In-Browser Regular Expression Lab

Base64 Regex — Validate Base64 Strings with Valid Padding

Validates Base64 encoded data with proper 4-character padding multiples using = characters.

Flags:
//none
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:SGVsbG8gV29ybGQ=

Test Samples Verification Matrix

Expected Valid Samples (Should Match)

SGVsbG8gV29ybGQ= Pass
Y2FzdG92 Pass
YW55IGNhcm5hbCBwbGVhc3VyZS4= Pass

Expected Invalid Samples (Must Reject)

SGVsbG8gV29ybGQ== Rejected (Pass)
Invalid#Characters Rejected (Pass)
SGVsbG8= Accepted (Fail)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
const text = "SGVsbG8gV29ybGQ=";

const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);

if (isMatch) {
  const matches = text.match(pattern);
  console.log("Matches:", matches);
}

Regular Expression Token Breakdown

Ensures string length is divisible by 4 and valid Base64 alphabet is followed with legal padding. Each component of the regular expression serves a distinct syntactical purpose as mapped below:

Token PatternName / ComponentFunction & Rule
[A-Za-z0-9+/]{4}Full 4-byte chunkStandard Base64 alphabet.
[A-Za-z0-9+/]{2}==2-pad endValid ending with 2 padding equals.
[A-Za-z0-9+/]{3}=1-pad endValid ending with 1 padding equal.

Frequently Asked Questions

What about Base64URL?
For Base64URL, replace `+` and `/` with `-` and `_`.
Browse All 40 Patterns