MIME Type in JS
Validate MIME media type strings like text/html, application/json, or image/png; charset=utf-8.
Try it in the JS tester →Pattern
regexJS
^([a-z]+)\/([a-z0-9][a-z0-9!#$&\-^_.]*)(?:;\s*[a-z]+=\S+)*$ (flags: i)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 matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern 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/jsonMatches
application/json
Input
text/html; charset=utf-8Matches
text/html; charset=utf-8
Input
not a mime typeNo match
—