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

TokenDescription
^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
\sMatches any whitespace character (space, tab, newline)
*Matches the preceding element zero or more times (greedy)
;Matches the literal character ';'
\sMatches 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

InputExpected
application/jsonMatch
text/html; charset=UTF-8Match
not-a-content-typeNo Match
image/svg+xmlMatch
multipart/form-data; boundary=abc123Match

Try It — Interactive Tester

//
gimsuy
No matches found.
Pattern: 74 charsFlags: noneMatches: 0

Ctrl+Shift+C to copy regex

Customize this pattern →