const regex = /(?<type>\w*)\/(?<tree>([\w\-]+\.)+)?(?<subtype>[\w\-]+)(\+(?<suffix>[\w\-\.]+))?(; (?<parameter>[\w+-\.=]+))?/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<type>\\w*)\\\/(?<tree>([\\w\\-]+\\.)+)?(?<subtype>[\\w\\-]+)(\\+(?<suffix>[\\w\\-\\.]+))?(; (?<parameter>[\\w+-\\.=]+))?', 'g')
const str = `text/html; charset=UTF-8
application/x-executable
application/graphql
application/javascript
application/json
application/ld+json
application/msword
application/pdf
application/sql
application/vnd.api+json
application/vnd.ms-excel
application/vnd.ms-powerpoint
application/vnd.oasis.opendocument.text
application/vnd.openxmlformats-officedocument.presentationml.presentation
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/x-www-form-urlencoded
application/xml
application/zip
application/zstd
audio/mpeg
audio/ogg
image/gif
image/apng
image/flif
image/webp
image/x-mng
image/jpeg
image/png
multipart/form-data
text/css
text/csv
text/html
text/php
text/plain
text/xml`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions