Validationflags: i

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

jsJavaScript
const re = new RegExp("^([a-z]+)\\/([a-z0-9][a-z0-9!#$&\\-^_.]*)(?:;\\s*[a-z]+=\\S+)*$", "i");
const input = "application/json";
const match = input.match(re);
console.log(match ? match[0] : null);

Uses `String.prototype.match`, which returns the first match or null.

Pattern

regexengine-agnostic
^([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

Group 1 captures the primary type (text, image, application, etc). Group 2 captures the subtype using the allowed MIME subtype character set. The optional trailing group (?:;\s*[a-z]+=\S+)* matches parameters like charset=utf-8 or boundary=something.

Examples

Input

application/json

Matches

  • application/json

Input

text/html; charset=utf-8

Matches

  • text/html; charset=utf-8

Input

not a mime type

No 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