Pattern library / Strings & Text
Regex to Match Base64 Strings
Validates the shape of a Base64 string including proper padding. Does not decode — pair with atob() / base64.b64decode in code.
The pattern
^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$
Load this pattern into the tester →
How the pattern works
(?:[A-Za-z0-9+/]{4})*— groups of 4 Base64 chars.- Padding handles the 1/2/3-byte remainders:
==for 1 byte,=for 2 bytes. - Use this for shape validation only; decode in code to verify content.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
SGVsbG8= | Match | Hello with padding | Try → |
SGVsbG8 | No match | missing padding for length 7 | Try → |
YWJj | Match | abc, no padding needed | Try → |
YWJjZA== | Match | abcd, two padding chars | Try → |
!!! | No match | invalid chars | Try → |
Where to use it
JavaScript
const re = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
re.test('SGVsbG8='); // true
Python
import re, base64
re.match(r'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$', 'SGVsbG8=')
Common mistakes
Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
FAQ
Related patterns
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.