Regex to Match Base64 Encoded Strings

Validate Base64 encoded strings with proper padding. Matches standard Base64 alphabet with correct trailing equals-sign padding. Free online regex tester.

Regular Expression

/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/m

Token Breakdown

TokenDescription
^Anchors at the start of the string (or line in multiline mode)
(?:Start of non-capturing group
[A-Za-z0-9+/]Character class — matches any one of: A-Za-z0-9+/
{4}Matches exactly 4 times
)End of group
*Matches the preceding element zero or more times (greedy)
(?:Start of non-capturing group
[A-Za-z0-9+/]Character class — matches any one of: A-Za-z0-9+/
{2}Matches exactly 2 times
=Matches the literal character '='
=Matches the literal character '='
|Alternation — matches the expression before OR after the pipe
[A-Za-z0-9+/]Character class — matches any one of: A-Za-z0-9+/
{3}Matches exactly 3 times
=Matches the literal character '='
)End of group
?Makes the preceding element optional (zero or one times)
$Anchors at the end of the string (or line in multiline mode)

Detailed Explanation

This regex validates strings encoded in standard Base64 format according to RFC 4648. Here is the token-by-token breakdown:

^ — Anchors the match at the start of a line, ensuring the entire string is validated from the beginning.

(?:[A-Za-z0-9+/]{4})* — A non-capturing group that matches zero or more complete 4-character Base64 blocks. The character class [A-Za-z0-9+/] contains the 64 characters of the standard Base64 alphabet: uppercase letters, lowercase letters, digits, plus sign, and forward slash. The {4} quantifier requires exactly four characters per block, and the * allows this to repeat for the full-length encoded data.

(?: — Opens a non-capturing group for the optional padding section.

[A-Za-z0-9+/]{2}== — First alternative: matches the final block when only one byte of data remains, producing two Base64 characters followed by two equals-sign padding characters.

| — Alternation operator.

[A-Za-z0-9+/]{3}= — Second alternative: matches the final block when two bytes of data remain, producing three Base64 characters followed by one equals-sign padding character.

)? — Makes the padding section optional, since some encoded strings have lengths that are exact multiples of three bytes and need no padding.

$ — Anchors the match at the end of a line.

The m flag enables multiline mode. This pattern validates that a string conforms to Base64 encoding rules including proper padding. It is useful for input validation, data format detection, and API request verification.

Example Test Strings

InputExpected
SGVsbG8gV29ybGQ=Match
dGVzdA==Match
not-base64!@#No Match
YWJjMatch
SGVsbG8=extraNo Match

Try It — Interactive Tester

//m
gimsuy

Match Highlighting(1 match)

SGVsbG8gV29ybGQ= dGVzdA== not-base64!@# YWJj SGVsbG8=extra

Matches & Capture Groups

#1SGVsbG8gV29ybGQ=index 0
Pattern: 64 charsFlags: mMatches: 1

Ctrl+Shift+C to copy regex

Customize this pattern →