Regex for Content-Type Header
Regex Pattern
^[a-zA-Z]+\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z-]+=\S+)*$HTTP Content-Type header with optional parameters
Quick Answer
The regex pattern for content-type header is `^[a-zA-Z]+\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z-]+=\S+)*$`. HTTP Content-Type header with optional parameters. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| text/html; charset=utf-8 | ✓ Matches |
| application/json | ✓ Matches |
| multipart/form-data; boundary=abc | ✓ Matches |
| text | ✗ No match |
| not a content type | ✗ No match |
| (empty) | ✗ No match |
Code Examples
javascript
const regex = /^[a-zA-Z]+\\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z-]+=\S+)*$/; const isValid = regex.test(value);
python
import re
pattern = r'^[a-zA-Z]+\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z-]+=\S+)*$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^[a-zA-Z]+\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z-]+=\S+)*$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^[a-zA-Z]+\\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z-]+=\S+)*$/', $value)) {
echo "valid";
}java
String pattern = "^[a-zA-Z]+\\/[a-zA-Z0-9.+-]+(?:\\s*;\\s*[a-zA-Z-]+=\\S+)*$"; 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
Data URL
Matches data: URL scheme with optional MIME type and base64 encoding