DD
DevDash

Regex for Base64 String (Strict)

Regex Pattern

^[A-Za-z0-9+/]{4,}(?:={0,2})$

Strict base64 encoded string (min 4 chars, proper padding)

Quick Answer

The regex pattern for base64 string (strict) is `^[A-Za-z0-9+/]{4,}(?:={0,2})$`. Strict base64 encoded string (min 4 chars, proper padding). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
SGVsbG8=✓ Matches
dGVzdA==✓ Matches
YWJjZGVmZw==✓ Matches
abc✗ No match
not*base64!✗ No match
===✗ No match

Code Examples

javascript

const regex = /^[A-Za-z0-9+\/]{4,}(?:={0,2})$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[A-Za-z0-9+/]{4,}(?:={0,2})$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[A-Za-z0-9+/]{4,}(?:={0,2})$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[A-Za-z0-9+\/]{4,}(?:={0,2})$/', $value)) {
    echo "valid";
}

java

String pattern = "^[A-Za-z0-9+/]{4,}(?:={0,2})$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.