New blogMaking an AI Agent You Can Trust With Money

★ Dev formats · regex

Regex for Base64

Matches standard (RFC 4648) base64 with correct padding. The empty string also matches, which is valid base64 for empty data. For base64url, swap + and / for - and _.

  • Runs in your browser
  • No signup

Try the base64 pattern

Read it in plain English (best-effort)

  • ^start of a line
  • (?:start a group (not captured)
  • [A-Za-z0-9+/]one character from: A–Z, a–z, 0–9, '+', '/'
  • {4}exactly 4 of the previous
  • )end of group
  • *zero or more of the previous
  • (?:start a group (not captured)
  • [A-Za-z0-9+/]one character from: A–Z, a–z, 0–9, '+', '/'
  • {2}exactly 2 of the previous
  • ==the text '=='
  • |or
  • [A-Za-z0-9+/]one character from: A–Z, a–z, 0–9, '+', '/'
  • {3}exactly 3 of the previous
  • =the character '='
  • )end of group
  • ?the previous is optional
  • $end of a line

One sample per line works well with the m flag on anchored patterns.

Waiting for a pattern
01

The pattern, piece by piece

/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/
Pattern explanation
PartWhat it does
(?:[A-Za-z0-9+/]{4})*full groups of four base64 characters
[A-Za-z0-9+/]{2}==a final group with two padding characters
|[A-Za-z0-9+/]{3}=or a final group with one padding character
02

What it accepts and rejects

Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.

Should match
Should match
SGVsbG8=✓
SGVsbG8gV29ybGQ=✓
YQ==✓
YWI=✓
YWJj✓
U29tZSBsb25nZXIgdGV4dCE+Lw==✓
Should not match
Should not match
SGVsbG8✗
YQ=✗
Y===✗
SGV sbG8=✗
SGVsbG8=$✗
YWJj=✗
03

Copy it into your code

RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.

const base64Re = /^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/;

export function isValidBase64(value) {
  return base64Re.test(value.trim());
}
04

When not to use regex for this

The simplest check is to decode it. atob() in JavaScript or base64.b64decode(…, validate=True) in Python fail loudly on bad input.

05

Questions engineers actually ask

Why does the empty string match?
Empty input encodes to an empty string, so it's valid base64. Add a length check if you need non-empty data.
Does it accept unpadded base64?
No. Unpadded variants are common in URLs and JWTs; use a base64url pattern for those.
What is catastrophic backtracking?
Some patterns, usually nested quantifiers like (a+)+, make a backtracking engine try an exponential number of paths on input that almost matches. The tester stops any run after 300ms, so a bad pattern can't freeze your tab. In production, that same pattern can take down a server (a ReDoS attack).
06

More dev formats patterns

07

More from the workbench

Fine print

Tools: on the house.
Engineer: by the hour.

The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.

I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.