This week I experimented multiple regexes and although they are hard to read, they are also quite powerful.
I wrote a command for AMLT to use regex to process strings from the chat.
the source code looks like this (since AMLT uses a completely different set of API than your regular browser, a lot of thing will look different):
let argc = getVar("argc");
if (argc < 2) {
console.error("Error: Please provide a regex pattern and a string to process.");
return;
}
let regexPattern = getVars("argv1");
let regex;
try {
regex = new RegExp(regexPattern, 'g');
} catch (e) {
console.error("Error: Invalid regex pattern.");
return;
}
let combinedString = [];
for (let i = 2; i < argc; i++) {
combinedString.push(getVars("argv" + i));
}
let inputString = combinedString.join(" ");
let matches = inputString.match(regex);
if (matches) {
MM.AddMessage(matches.join(", "),5000);
} else {
MM.AddMessage("No matches found.",3000);
}
Code language: JavaScript (javascript)
Leave a Reply