So I've got a regular expression parsing tcp responses from a music daemon. In case responses pile up I've come up with an expression to separate full responses from the data, however I'd like to also reference which choice was matched. Using capturing groups results in different numbers for each group, so is there a way to reference which alternation choice was matched besides that?
var patt = /((volume):\s\d+\n(.+\n)+?OK\n|OK\s(MPD)\s\d+\.\d+\.\d+|(OK)|(ACK).+\n)/;
This will match it, but I want to do different things with the separated response based on which match it was. I'd like to just have a switch case reference a single variable, but the only way I can think of to is:
var mycase = null; var result = patt.exec(data); if (result == "volume") mycase= "volume"; else if (result == "MPD") mycase= "MPD"; else if (result == "OK") mycase= "OK"; else if (result == "ACK") mycase= "ACK";
Is there a better way to do this?