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.
| Input | Result | Matched |
|---|---|---|
| aGVsbG8= | matches | aGVsbG8= |
| aGVsbG9v | matches | aGVsbG9v |
| YQ== | matches | YQ== |
| aGVsbG8 | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID