Base64 String Regex

^[A-Za-z0-9+/]+={0,2}$

Match Base64-encoded strings with proper padding.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern ^[A-Za-z0-9+/]+={0,2}$:

  • ^ - Start of string
  • $ - End of string
  • + - One or more
  • [...] - Character class
  • {n,m} - Quantifier (n to m times)

Examples

Input Result Explanation
SGVsbG8gV29ybGQ= ✓ Match Hello World encoded
YWJj ✓ Match abc encoded (no padding)

Code Examples

JavaScript

JavaScript
const pattern = /^[A-Za-z0-9+\/]+={0,2}$/;
const text = "SGVsbG8gV29ybGQ=";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'^[A-Za-z0-9+/]+={0,2}$'
text = "SGVsbG8gV29ybGQ="

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/^[A-Za-z0-9+\/]+={0,2}$/';
$text = "SGVsbG8gV29ybGQ=";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "^[A-Za-z0-9+/]+={0,2}$";
String text = "SGVsbG8gV29ybGQ=";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Data encoding, file uploads, API payloads

Frequently Asked Questions

What is the regex pattern for base64 string regex?
The regex pattern is: ^[A-Za-z0-9+/]+={0,2}$. Match Base64-encoded strings with proper padding.
How do I use the base64 string regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^[A-Za-z0-9+/]+={0,2}$'); or const regex = /^[A-Za-z0-9+\/]+={0,2}$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^[A-Za-z0-9+/]+={0,2}$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for base64 string regex?
Data encoding, file uploads, API payloads

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: