Code
Base64 encoded string Regex
Base64 encodes binary data as text using 64 characters plus = for padding. A standard encoded string has a length that is a multiple of four, and padding can only appear at the end as one or two = signs. This pattern checks exactly that structure and refuses empty strings. It is useful for validating data URIs, API fields that carry file contents and configuration values that should be encoded. The honest limit is that many ordinary words, such as test or ABCD, are also valid Base64, so a match only says the text could be decoded. It does not cover the URL-safe alphabet with - and _.
Pattern (JavaScript)
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$/Starts at the start of the line/string, then (one of the characters A-Za-z0-9+/, exactly 4 times), zero or more times, then (one of the characters A-Za-z0-9+/, exactly 4 times OR one of the characters A-Za-z0-9+/, exactly 2 times, then the text "==" OR one of the characters A-Za-z0-9+/, exactly 3 times, then the character "="), then the end of the line/string.
Limit: Checks the encoding shape only; ordinary words of the right length also match.
Examples
Matches
- ✓ SGVsbG8=
- ✓ dGVzdA==
- ✓ UmVnZXhMYWI=
- ✓ QUJD
Does not match
- ✗ SGVsbG8
- ✗ abc$
- ✗ SGVsbG8===
- ✗ dGVz dA==
Try it
0 matches (JavaScript engine)
SGVsbG8=
dGVzdA==
UmVnZXhMYWI=
QUJD
SGVsbG8
abc$
SGVsbG8===
dGVz dA==Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)$/g;
const text = "SGVsbG8=";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$')
text = 'SGVsbG8='
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$#';
$text = 'SGVsbG8=';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$");
String text = "SGVsbG8=";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$`)
text := "SGVsbG8="
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$");
var text = "SGVsbG8=";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$").unwrap();
let text = "SGVsbG8=";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{4}|[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)$/
text = 'SGVsbG8='
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must the length be a multiple of four?
- Base64 turns every 3 bytes into 4 characters, and padding fills the final group. A correctly padded string therefore always has a length divisible by four.
- How do I accept URL-safe Base64?
- Replace + and / with - and _ in every character class, and make the padding optional because URL-safe encoders often drop it. JWTs use that variant.
- Why does a plain word like test match?
- Any four characters from the alphabet are valid Base64, so short words decode to some bytes. Combine this check with a minimum length or a decode attempt if false positives matter.