MIME Type
Validate MIME media type strings like text/html, application/json, or image/png; charset=utf-8.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("^([a-z]+)\\/([a-z0-9][a-z0-9!#$&\\-^_.]*)(?:;\\s*[a-z]+=\\S+)*$", "i");
const input = "application/json";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"^([a-z]+)\/([a-z0-9][a-z0-9!#$&\-^_.]*)(?:;\s*[a-z]+=\S+)*$", re.IGNORECASE)
input_text = "application/json"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)^([a-z]+)\/([a-z0-9][a-z0-9!#$&\-^_.]*)(?:;\s*[a-z]+=\S+)*$`)
input := `application/json`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
^([a-z]+)\/([a-z0-9][a-z0-9!#$&\-^_.]*)(?:;\s*[a-z]+=\S+)*$ (flags: i)Raw source: ^([a-z]+)\/([a-z0-9][a-z0-9!#$&\-^_.]*)(?:;\s*[a-z]+=\S+)*$
How it works
Examples
Input
application/jsonMatches
application/json
Input
text/html; charset=utf-8Matches
text/html; charset=utf-8
Input
not a mime typeNo match
—Common use cases
- •Content-Type header validation in API middleware
- •File upload MIME type enforcement
- •HTTP response content negotiation
- •OpenAPI spec validation tooling
Related patterns
BCP 47 Language Tag
ValidationValidate BCP 47 language tags like `en`, `en-US`, `zh-Hant-TW`, or `pt-BR`.
Alphanumeric Only
ValidationMatch strings containing only letters (A–Z, a–z) and digits (0–9), with no spaces or special characters.
Cron Expression
ValidationValidate standard 5-field Unix cron expressions: minute, hour, day-of-month, month, day-of-week.
Cron Expression (Quartz/Spring, 6-Field)
ValidationValidate 6-field Quartz/Spring cron expressions including the seconds field at the front.