Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for base64 string
Regex pattern · validation

Regex for base64 string

Regex for base64: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/ as a base64 string regex. It matches "SGVsbG8=" and "SGVsbG8gV29ybGQ=", and rejects "SGVsbG8" and "SGV$bG8=". An empty string also matches (it is valid, empty base64).

Last updated · Maintained by FreeAIToolz · How we test

/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/
  • SGVsbG8=matches
  • SGVsbG8gV29ybGQ=matches
  • TWFumatches
  • SGVsbG8no match
  • SGV$bG8=no match
  • SGVsbG8===no match
  • SGVsbG8=ano match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
(?:[A-Za-z0-9+/]{4})*any number of full 4-character blocks
[A-Za-z0-9+/]{2}==or a final block with two padding characters
[A-Za-z0-9+/]{3}=or one padding character

Examples

InputResult
SGVsbG8=✓ Matches
SGVsbG8gV29ybGQ=✓ Matches
TWFu✓ Matches
SGVsbG8✗ No match
SGV$bG8=✗ No match
SGVsbG8===✗ No match
SGVsbG8=a✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

An empty string also matches (it is valid, empty base64). URL-safe base64 uses - and _ instead of + and /, often without padding.

When to use this pattern

Checking data before decoding in APIs, data URIs and config files.

Use it in your language

JavaScript

const re = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for base64 string?
/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/. An empty string also matches (it is valid, empty base64). URL-safe base64 uses - and _ instead of + and /, often without padding.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abcSGVsbG8=xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.