(() => {
const regExps = [
'(?<who>.+) pierces (?<whom>.+) very hard\\.?',
'(?<who>.+) pierces (?<whom>.+) extremely hard\\.?',
'(?<who>.+) pierces (?<whom>.+)\\.?',
'(?<who>.+) barely pierces (?<whom>.+)\\.?',
].sort((a, b) => b.length - a.length).map(e => new RegExp(e, 'iu'));
const messages = [
"Bobby pierces an elite guard very hard.",
"Bobby pierces an elite guard extremely hard.",
"Bobby pierces an elite guard.",
"Bobby barely pierces an elite guard.",
"An elite guard pierces Bobby very hard.",
"An elite guard pierces Bobby extremely hard.",
"An elite guard pierces Bobby.",
"An elite guard barely pierces Bobby.",
"Somebody barely pierces Topic starter."
];
function getActors(message) {
for (let i = 0; i < regExps.length; i++) {
const matches = message.match(regExps[i]);
if (matches === null) {
continue;
}
return matches.groups;
}
return null;
}
messages.forEach(message => {
const actors = getActors(message);
if (actors === null) {
alert(`«${message}»:\nunknown`);
return;
}
alert(`«${message}»:\n${actors.who} -> ${actors.whom}`);
});
})()