From d5002863e0863d5523c435a071cb3492c62c8f5f Mon Sep 17 00:00:00 2001 From: Crow Date: Mon, 28 Apr 2025 11:03:27 +0100 Subject: [PATCH 1/7] Add both `test` and `match` regex commands These commands match the behavior of the javascript `Regex.test()` and `String.match()` / `String.matchAll()` functions. --- public/scripts/slash-commands.js | 80 ++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) 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', From 729830c2fc443867f1cc83055f2a53711e3763ce Mon Sep 17 00:00:00 2001 From: Crow Date: Tue, 29 Apr 2025 06:19:59 +0100 Subject: [PATCH 2/7] Validate pattern --- public/scripts/slash-commands.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 99810d98b..b2f232149 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2134,10 +2134,13 @@ export function initDefaultSlashCommands() { SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'test', callback: (({ pattern }, text) => { - if (pattern === '') { + if (!pattern) { throw new Error('Argument of \'pattern=\' cannot be empty'); } let 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', @@ -2170,10 +2173,13 @@ export function initDefaultSlashCommands() { SlashCommandParser.addCommandObject(SlashCommand.fromProps({ name: 'match', callback: (({ pattern }, text) => { - if (pattern === '') { + if (!pattern) { throw new Error('Argument of \'pattern=\' cannot be empty'); } let 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 { From 1c40ea10f45d58e2727d08df395cc89170a276a9 Mon Sep 17 00:00:00 2001 From: Crow Date: Tue, 29 Apr 2025 06:37:08 +0100 Subject: [PATCH 3/7] Format examples correctly --- public/scripts/slash-commands.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index b2f232149..97120ed92 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2163,10 +2163,10 @@ export function initDefaultSlashCommands() {
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   ||
+
/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   ||
`, })); @@ -2208,12 +2208,12 @@ export function initDefaultSlashCommands() {
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  |/# []                                                        ||
+
/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  |/# []                                                        ||
`, })); From 5ddc8f17a0958e5bd8bc42475955047ed729dd2b Mon Sep 17 00:00:00 2001 From: Crow Date: Tue, 29 Apr 2025 06:37:48 +0100 Subject: [PATCH 4/7] Fix pattern checking on /replace --- public/scripts/slash-commands.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 97120ed92..872a5eb68 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); From 0cd0ce237479541a09a6ae37058053e8dba4e056 Mon Sep 17 00:00:00 2001 From: Crow Date: Tue, 29 Apr 2025 06:38:30 +0100 Subject: [PATCH 5/7] Fix example formatting on /replace --- public/scripts/slash-commands.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 872a5eb68..047c28d29 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2124,11 +2124,11 @@ 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   ||
`, })); From 6cb1eb3fe69cfebd2c71138c6b0971007007acc4 Mon Sep 17 00:00:00 2001 From: Crow Date: Tue, 29 Apr 2025 15:21:45 +0100 Subject: [PATCH 6/7] Change return to empty string on no single match --- public/scripts/slash-commands.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 047c28d29..fab878e7a 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2177,14 +2177,15 @@ export function initDefaultSlashCommands() { if (!pattern) { throw new Error('Argument of \'pattern=\' cannot be empty'); } - let re = regexFromString(pattern.toString()); + 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 { - return JSON.stringify(text.toString().match(re)); + const match = text.toString().match(re); + return match ? JSON.stringify(match) : ''; } }), returns: 'group array for each match', @@ -2205,7 +2206,7 @@ export function initDefaultSlashCommands() {
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. + otherwise it returns an empty string.
Example: @@ -2213,7 +2214,7 @@ export function initDefaultSlashCommands() {
/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" {{var::x}}           | /echo  |/#                                                           ||
/match pattern="/orange/g" {{var::x}}        | /echo  |/# []                                                        ||
`, From 6aeced98a6a1fdbebc0dd6110119f8f8424904c3 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 29 Apr 2025 21:22:51 +0300 Subject: [PATCH 7/7] Prefer const. I love const --- public/scripts/slash-commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index fab878e7a..66555216c 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -2138,7 +2138,7 @@ export function initDefaultSlashCommands() { if (!pattern) { throw new Error('Argument of \'pattern=\' cannot be empty'); } - let re = regexFromString(pattern.toString()); + const re = regexFromString(pattern.toString()); if (!re) { throw new Error('The value of \'pattern\' argument is not a valid regular expression.'); }