Regex for Base64 String
Validates standard Base64-encoded strings with optional padding.
Pattern
/^[A-Za-z0-9+/]+(={0,2})$/ Live Tester
Enter a string to test
Examples
✓ SGVsbG8gV29ybGQ=
✓ dGVzdA==
✓ YWJj
✗ SGVsbG8!
✗ not base64 here
✗ ===
Pattern Breakdown
^[A-Za-z0-9+/]+ — one or more Base64 alphabet characters
(={0,2})$ — zero, one, or two padding = characters
Code Snippets
Javascript
const regex = /^[A-Za-z0-9+/]+(={0,2})$/;
regex.test("SGVsbG8gV29ybGQ="); // true Python
import re
pattern = r"^[A-Za-z0-9+/]+(={0,2})$"
bool(re.match(pattern, "SGVsbG8gV29ybGQ=")) # True Php
$pattern = '/^[A-Za-z0-9+\/]+(={0,2})$/';
preg_match($pattern, "SGVsbG8gV29ybGQ="); // 1 Related Patterns
Email Address /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ URL /^https?:\/\/(www\.)?[a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([a-zA-Z0-9()@:%_+.~#?&/=-]*)$/ Phone Number (International) /^\+?[1-9]\d{1,14}$/ Phone Number (US) /^\(?[2-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/ Phone Number (EU) /^\+?[1-9][0-9]{0,3}[\s.-]?\(?[0-9]{1,5}\)?[\s.-]?[0-9]{1,5}[\s.-]?[0-9]{1,5}$/ IPv4 Address /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
Frequently Asked Questions
What is the regex for Base64 String?▾
The regex pattern for Base64 String is /^[A-Za-z0-9+/]+(={0,2})$/. Validates standard Base64-encoded strings with optional padding.
How do I use the Base64 String regex in JavaScript?▾
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Base64 String pattern match?▾
It matches strings like SGVsbG8gV29ybGQ=; it rejects strings like SGVsbG8!. See the Examples section above for the full list of matching and non-matching cases.