Regex to Match Content-Type Headers
Validate MIME content type strings with optional parameters like charset. Matches types like text/html, application/json, and multipart/form-data.
Regular Expression
/^[a-zA-Z]+\/[a-zA-Z0-9.+-]+(?:\s*;\s*[a-zA-Z0-9-]+=(?:"[^"]*"|[^;,\s]+))*$/
Token Breakdown
| Token | Description |
|---|---|
| ^ | Anchors at the start of the string (or line in multiline mode) |
| [a-zA-Z] | Character class — matches any one of: a-zA-Z |
| + | Matches the preceding element one or more times (greedy) |
| \/ | Matches a literal forward slash |
| [a-zA-Z0-9.+-] | Character class — matches any one of: a-zA-Z0-9.+- |
| + | Matches the preceding element one or more times (greedy) |
| (?: | Start of non-capturing group |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| ; | Matches the literal character ';' |
| \s | Matches any whitespace character (space, tab, newline) |
| * | Matches the preceding element zero or more times (greedy) |
| [a-zA-Z0-9-] | Character class — matches any one of: a-zA-Z0-9- |
| + | Matches the preceding element one or more times (greedy) |
| = | Matches the literal character '=' |
| (?: | Start of non-capturing group |
| " | Matches the literal character '"' |
| [^"] | Negated character class — matches any character NOT in " |
| * | Matches the preceding element zero or more times (greedy) |
| " | Matches the literal character '"' |
| | | Alternation — matches the expression before OR after the pipe |
| [^;,\s] | Negated character class — matches any character NOT in ;,\s |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| ) | End of group |
| * | Matches the preceding element zero or more times (greedy) |
| $ | Anchors at the end of the string (or line in multiline mode) |
Detailed Explanation
This regex validates MIME content type strings as used in HTTP Content-Type headers. Here is the token-by-token breakdown:
^ — Anchors the match at the start of the string.
[a-zA-Z]+ — Matches the top-level media type. This is always a word like text, image, application, audio, video, multipart, or font.
/ — Matches the literal forward slash separating the type from the subtype.
[a-zA-Z0-9.+-]+ — Matches the media subtype. Subtypes can contain letters, digits, dots, plus signs, and hyphens. Examples include html, json, xml, vnd.ms-excel, svg+xml, and x-www-form-urlencoded.
(?:\s*;\s*[a-zA-Z0-9-]+=(?:"[^"]"|[^;,\s]+)) — Zero or more parameter sections. Each starts with an optional-whitespace-surrounded semicolon, followed by a parameter name consisting of letters, digits, and hyphens, an equals sign, and the parameter value which can be either a quoted string or an unquoted token. Common parameters include charset=UTF-8 and boundary=something.
$ — Anchors the match at the end of the string.
No flags are used since this validates a single complete content type string. This pattern is useful for HTTP header validation, file upload processing, API request validation, and content negotiation systems. It correctly handles simple types like text/plain and complex types with parameters like multipart/form-data;boundary=----WebKitFormBoundary.
Example Test Strings
| Input | Expected |
|---|---|
| application/json | Match |
| text/html; charset=UTF-8 | Match |
| not-a-content-type | No Match |
| image/svg+xml | Match |
| multipart/form-data; boundary=abc123 | Match |
Try It — Interactive Tester
74 charsFlags: noneMatches: 0Ctrl+Shift+C to copy regex