JSFiddle - React, Tailwind, and code Playground
JavaScript
const input = "categories fruit meat mandatory";
const regex = /\w+/gm;
const regex2 = /^\w+/g;
const results = input.matchAll(regex);
let type = input.match(regex2)[0];
let isMandatory = false;
let isCategory = false;
let categories = [];
for (const result of results){
switch(result[0]){
case "categories": isCategory = true; break;
case "mandatory": isMandatory = true; break;
default: categories.push(result[0]);
}
}
console.log("Type: " + type);
console.log("Categories: " + categories.join(", "));
console.log("Mandatory: " + (isMandatory ? "Yes" : "No"));