Regex for Data URL
Regex Pattern
^data:([a-zA-Z]+\/[a-zA-Z0-9.+-]+)?(;base64)?,Matches data: URL scheme with optional MIME type and base64 encoding
Quick Answer
The regex pattern for data url is `^data:([a-zA-Z]+\/[a-zA-Z0-9.+-]+)?(;base64)?,`. Matches data: URL scheme with optional MIME type and base64 encoding. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| data:text/plain,Hello | ✓ Matches |
| data:image/png;base64,iVBOR | ✓ Matches |
| data:,plaintext | ✓ Matches |
| http://example.com | ✗ No match |
| data: | ✗ No match |
| not-a-data-url | ✗ No match |
Code Examples
javascript
const regex = /^data:([a-zA-Z]+\\/[a-zA-Z0-9.+-]+)?(;base64)?,/; const isValid = regex.test(value);
python
import re
pattern = r'^data:([a-zA-Z]+\/[a-zA-Z0-9.+-]+)?(;base64)?,'
if re.match(pattern, value):
print("valid")ruby
pattern = /^data:([a-zA-Z]+\/[a-zA-Z0-9.+-]+)?(;base64)?,/ if value =~ pattern puts "valid" end
php
if (preg_match('/^data:([a-zA-Z]+\\/[a-zA-Z0-9.+-]+)?(;base64)?,/', $value)) {
echo "valid";
}java
String pattern = "^data:([a-zA-Z]+\\/[a-zA-Z0-9.+-]+)?(;base64)?,"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
Subdomain Extraction
Extracts the subdomain from a URL or domain name
Protocol Detection
Detects and captures the protocol from a URL
Port Number
Extracts port number from a URL or host string
Query Parameter
Extracts key-value pairs from URL query strings
HTTP vs HTTPS URL
Matches HTTP or HTTPS URLs and captures the protocol
WebSocket URL
WebSocket URL starting with ws:// or wss://