Back to all patterns
Base64 String Validation
Validation
Validates standard Base64-encoded strings including padding.
/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/How it works
Base64 encodes binary data as ASCII text using 64 characters (A-Z, a-z, 0-9, +, /). The encoded string length is always a multiple of 4, with = padding characters added as needed. This pattern validates standard Base64, not Base64URL (which uses - and _ instead of + and /).
Test Cases
Should Match
- SGVsbG8gV29ybGQ=
- dGVzdA==
- YWJj
Should NOT Match
- SGVsbG8gV29ybGQ
- not-base64!
- SGVsbG8gV29ybGQ===
Quick Summary
Validates standard Base64-encoded strings with correct padding. Does not validate Base64URL (which uses - and _ instead of + and /). An empty string also matches, add a minimum length check if needed.
Key Takeaways
Key Takeaways
- Standard Base64 uses A-Z, a-z, 0-9, +, and / with = padding
- Base64URL uses - and _ instead of + and /, use a different pattern for JWTs
- Encoded length is always a multiple of 4 (with padding)
- An empty string matches this pattern, add {1,} if you need at least one character
Use Cases
When to use it
- Validating Base64-encoded file uploads or data URIs
- Checking encoded credentials in Authorization headers
- Verifying Base64-encoded configuration values
Watch out
Common Mistakes
- Using this for Base64URL (JWT segments), JWTs use - and _ not + and /
- Forgetting that valid Base64 can decode to binary data that is not valid UTF-8
FAQ
Base64 String Validation, Frequently Asked
What is the difference between Base64 and Base64URL?
Base64URL replaces + with - and / with _ to make the encoding safe for URLs and filenames. JWTs use Base64URL.
How do I decode Base64 in JavaScript?
Use atob() in browsers or Buffer.from(str, 'base64').toString() in Node.js.