RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex for a Base64 string

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

Base64 encodes three bytes into four characters, so the body comes in groups of four, and the tail is padded to that length with one or two equals signs. URL-safe Base64 swaps + and / for - and _; change the class if that is what you are handling.

What each part does

[A-Za-z0-9+/]{4}one full group of four
(?: … )*any number of those groups
{2}==a two-character tail padded with two equals signs
{3}=or a three-character tail padded with one

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
aGVsbG8=matchesaGVsbG8=
aGVsbG9vmatchesaGVsbG9v
YQ==matchesYQ==
aGVsbG8no match
aGVsbG8==no match
aGVs bG8=no match

The mistake to avoid

Assuming a match means the content is safe. Base64 is an encoding, not a validation — decode it and then check what came out.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers