diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index ae1523039..66555216c 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2079,8 +2079,9 @@ export function initDefaultSlashCommands() { name: 'replace', aliases: ['re'], callback: (async ({ mode = 'literal', pattern, replacer = '' }, text) => { - if (pattern === '') + if (!pattern) { throw new Error('Argument of \'pattern=\' cannot be empty'); + } switch (mode) { case 'literal': return text.replaceAll(pattern, replacer); @@ -2123,11 +2124,98 @@ export function initDefaultSlashCommands() {
Example: -
/let x Blue house and blue car ||                                                                        
-
/replace pattern="blue" {{var::x}}                                | /echo  |/# Blue house and  car     ||
-
/replace pattern="blue" replacer="red" {{var::x}}                 | /echo  |/# Blue house and red car  ||
-
/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}}   | /echo  |/# red house and blue car  ||
-
/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}}  | /echo  |/# red house and red car   ||
+
/let x Blue house and blue car ||                                                                        
+
/replace pattern="blue" {{var::x}}                                | /echo  |/# Blue house and  car     ||
+
/replace pattern="blue" replacer="red" {{var::x}}                 | /echo  |/# Blue house and red car  ||
+
/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}}   | /echo  |/# red house and blue car  ||
+
/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}}  | /echo  |/# red house and red car   ||
+
+ `, + })); + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ + name: 'test', + callback: (({ pattern }, text) => { + if (!pattern) { + throw new Error('Argument of \'pattern=\' cannot be empty'); + } + const re = regexFromString(pattern.toString()); + if (!re) { + throw new Error('The value of \'pattern\' argument is not a valid regular expression.'); + } + 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'); + } + const re = regexFromString(pattern.toString()); + if (!re) { + throw new Error('The value of \'pattern\' argument is not a valid regular expression.'); + } + if (re.flags.includes('g')) { + return JSON.stringify([...text.toString().matchAll(re)]); + } else { + const match = text.toString().match(re); + return match ? JSON.stringify(match) : ''; + } + }), + 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 an empty string. +
+
+ 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  |/#                                                           ||
+
/match pattern="/orange/g" {{var::x}}        | /echo  |/# []                                                        ||
`, }));