HTTP Content-Type Header
Parse the value of an HTTP Content-Type header, capturing the media type and optional charset.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("Content-Type:\\s*([a-z]+\\/[\\w.+\\-]+)(?:;\\s*charset=([\\w\\-]+))?", "gi");
const input = "Content-Type: text/html; charset=utf-8";
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"Content-Type:\s*([a-z]+\/[\w.+\-]+)(?:;\s*charset=([\w\-]+))?", re.IGNORECASE)
input_text = "Content-Type: text/html; charset=utf-8"
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)Content-Type:\s*([a-z]+\/[\w.+\-]+)(?:;\s*charset=([\w\-]+))?`)
input := `Content-Type: text/html; charset=utf-8`
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
Content-Type:\s*([a-z]+\/[\w.+\-]+)(?:;\s*charset=([\w\-]+))? (flags: gi)Raw source: Content-Type:\s*([a-z]+\/[\w.+\-]+)(?:;\s*charset=([\w\-]+))?
How it works
Examples
Input
Content-Type: text/html; charset=utf-8Matches
Content-Type: text/html; charset=utf-8
Input
content-type: application/jsonMatches
content-type: application/json
Input
Authorization: Basic xyzNo match
—Common use cases
- •Reverse-proxy / WAF rule matching
- •API gateway request inspection
- •HTTP log analysis
- •Content-negotiation debugging
Related patterns
Cookie Header Value
WebParse name=value pairs from an HTTP `Cookie:` header value.
CSS hsl() / hsla() Color
WebMatch CSS hsl() and hsla() color functions, capturing hue (0–360), saturation, lightness, and optional alpha.
HTML Attribute
WebMatch HTML attributes of the form name="value" or name='value' and capture both parts.
HTML5 Color Input Value
WebValidate the value of an HTML5 `<input type="color">` — exactly 6 hex digits with leading hash.