diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index ae1523039..99810d98b 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2131,6 +2131,86 @@ export function initDefaultSlashCommands() { `, })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'test', + callback: (({ pattern }, text) => { + if (pattern === '') { + throw new Error('Argument of \'pattern=\' cannot be empty'); + } + let re = regexFromString(pattern.toString()); + return JSON.stringify(re.test(text.toString())); + }), + returns: 'true | false', + namedArgumentList: [ + new SlashCommandNamedArgument( + 'pattern', 'pattern to find', [ARGUMENT_TYPE.STRING], true, false, + ), + ], + unnamedArgumentList: [ + new SlashCommandArgument( + 'text to test', [ARGUMENT_TYPE.STRING], true, false, + ), + ], + helpString: ` +
+ Tests text for a regular expression match. +
+
+ Returns true if the match is found, false otherwise. +
+
+ Example: +
/let x Blue house and green car                         ||
+
/test pattern="green" {{var::x}}    | /echo  |/# true   ||
+
/test pattern="blue" {{var::x}}     | /echo  |/# false  ||
+
/test pattern="/blue/i" {{var::x}}  | /echo  |/# true   ||
+
+ `, + })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'match', + callback: (({ pattern }, text) => { + if (pattern === '') { + throw new Error('Argument of \'pattern=\' cannot be empty'); + } + let re = regexFromString(pattern.toString()); + if (re.flags.includes('g')) { + return JSON.stringify([...text.toString().matchAll(re)]); + } else { + return JSON.stringify(text.toString().match(re)); + } + }), + returns: 'group array for each match', + namedArgumentList: [ + new SlashCommandNamedArgument( + 'pattern', 'pattern to find', [ARGUMENT_TYPE.STRING], true, false, + ), + ], + unnamedArgumentList: [ + new SlashCommandArgument( + 'text to match against', [ARGUMENT_TYPE.STRING], true, false, + ), + ], + helpString: ` +
+ Retrieves regular expression matches in the given text +
+
+ Returns an array of groups (with the first group being the full match). If the regex contains the global flag (i.e. /g), + multiple nested arrays are returned for each match. If the regex is global, returns [] if no matches are found, + otherwise it returns null. +
+
+ Example: +
/let x color_green green lamp color_blue                                                                            ||
+
/match pattern="green" {{var::x}}            | /echo  |/# [ "green" ]                                               ||
+
/match pattern="color_(\\w+)" {{var::x}}     | /echo  |/# [ "color_green", "green" ]                                ||
+
/match pattern="/color_(\\w+)/g" {{var::x}}  | /echo  |/# [ [ "color_green", "green" ], [ "color_blue", "blue" ] ]  ||
+
/match pattern="orange" {{var::x}}           | /echo  |/# null                                                      ||
+
/match pattern="/orange/g" {{var::x}}        | /echo  |/# []                                                        ||
+
+ `, + })); SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'chat-jump',