Web
Data URI Regex
Data URIs embed small files directly in HTML, CSS or JSON, for example data:image/png;base64,iVBORw0KGgo=. The format is the data: scheme, an optional media type, optional parameters such as charset=utf-8, an optional ;base64 marker and then a comma followed by the content. This pattern checks that structure and captures the media type in group 1. It is useful for spotting inline images in stylesheets, limiting uploads in rich text editors and auditing pages for large embedded assets. The honest limit is that it does not decode the payload or confirm it matches the declared type.
Pattern (JavaScript)
/^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$/Starts at the start of the line/string, then the text "data:", then a group (a lowercase letter, one or more times, then the character "/", then one of the characters a-z0-9.+-, one or more times), optionally (zero or one time), then (the character ";", then one of the characters a-z0-9-, one or more times, then the character "=", then one of the characters a-z0-9-, one or more times), zero or more times, then a group (the text ";base64"), optionally (zero or one time), then the character ",", then one of the characters A-Za-z0-9+/=%._~-, zero or more times, then the end of the line/string.
Examples
Matches
- ✓ data:image/png;base64,iVBORw0KGgo=
- ✓ data:text/plain;charset=utf-8,Hello%20World
- ✓ data:,Hello
Does not match
- ✗ data:image/png;base64
- ✗ https://example.com/a.png
- ✗ data:image/png;base64,abc def
Try it
0 matches (JavaScript engine)
data:image/png;base64,iVBORw0KGgo=
data:text/plain;charset=utf-8,Hello%20World
data:,Hello
data:image/png;base64
https://example.com/a.png
data:image/png;base64,abc defCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^data:([a-z]+\/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+\/=%._~-]*$/g;
const text = "data:image/png;base64,iVBORw0KGgo=";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$')
text = 'data:image/png;base64,iVBORw0KGgo='
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$#';
$text = 'data:image/png;base64,iVBORw0KGgo=';
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("^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$");
String text = "data:image/png;base64,iVBORw0KGgo=";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$`)
text := "data:image/png;base64,iVBORw0KGgo="
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$");
var text = "data:image/png;base64,iVBORw0KGgo=";
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"^data:([a-z]+/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+/=%._~-]*$").unwrap();
let text = "data:image/png;base64,iVBORw0KGgo=";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^data:([a-z]+\/[a-z0-9.+-]+)?(?:;[a-z0-9-]+=[a-z0-9-]+)*(;base64)?,[A-Za-z0-9+\/=%._~-]*$/
text = 'data:image/png;base64,iVBORw0KGgo='
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are spaces rejected in the content?
- Data URIs must not contain raw spaces; they should be percent-encoded as %20. A space usually means the URI was broken across lines or copied incorrectly.
- How do I block SVG data URIs for security?
- Check capture group 1 after a match and reject image/svg+xml, because SVG can contain scripts. Allow-listing types such as image/png and image/jpeg is safer than block-listing.
- Are uppercase media types accepted?
- Not as written, since media types are conventionally lowercase. Add the i flag if your data may contain values like IMAGE/PNG.