Add both `test` and `match` regex commands These commands match the behavior of the javascript `Regex.test()` and `String.match()` / `String.matchAll()` functions.

d5002863e0863d5523c435a071cb3492c62c8f5f

Crow <bismuthglass@protonmail.com>

1 files changed, +80 -0Showing whitespace changes
public/scripts/slash-commands.js+80 -0
@@ -2131,6 +2131,86 @@ export function initDefaultSlashCommands() {
21312131 </div>
21322132 `,
21332133 }));
2134+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2135+ name: 'test',
2136+ callback: (({ pattern }, text) => {
2137+ if (pattern === '') {
2138+ throw new Error('Argument of \'pattern=\' cannot be empty');
2139+ }
2140+ let re = regexFromString(pattern.toString());
2141+ return JSON.stringify(re.test(text.toString()));
2142+ }),
2143+ returns: 'true | false',
2144+ namedArgumentList: [
2145+ new SlashCommandNamedArgument(
2146+ 'pattern', 'pattern to find', [ARGUMENT_TYPE.STRING], true, false,
2147+ ),
2148+ ],
2149+ unnamedArgumentList: [
2150+ new SlashCommandArgument(
2151+ 'text to test', [ARGUMENT_TYPE.STRING], true, false,
2152+ ),
2153+ ],
2154+ helpString: `
2155+ <div>
2156+ Tests text for a regular expression match.
2157+ </div>
2158+ <div>
2159+ Returns <code>true</code> if the match is found, <code>false</code> otherwise.
2160+ </div>
2161+ <div>
2162+ <strong>Example:</strong>
2163+ <pre>/let x Blue house and green car ||</pre>
2164+ <pre>/test pattern="green" {{var::x}} | /echo |/# true ||</pre>
2165+ <pre>/test pattern="blue" {{var::x}} | /echo |/# false ||</pre>
2166+ <pre>/test pattern="/blue/i" {{var::x}} | /echo |/# true ||</pre>
2167+ </div>
2168+ `,
2169+ }));
2170+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2171+ name: 'match',
2172+ callback: (({ pattern }, text) => {
2173+ if (pattern === '') {
2174+ throw new Error('Argument of \'pattern=\' cannot be empty');
2175+ }
2176+ let re = regexFromString(pattern.toString());
2177+ if (re.flags.includes('g')) {
2178+ return JSON.stringify([...text.toString().matchAll(re)]);
2179+ } else {
2180+ return JSON.stringify(text.toString().match(re));
2181+ }
2182+ }),
2183+ returns: 'group array for each match',
2184+ namedArgumentList: [
2185+ new SlashCommandNamedArgument(
2186+ 'pattern', 'pattern to find', [ARGUMENT_TYPE.STRING], true, false,
2187+ ),
2188+ ],
2189+ unnamedArgumentList: [
2190+ new SlashCommandArgument(
2191+ 'text to match against', [ARGUMENT_TYPE.STRING], true, false,
2192+ ),
2193+ ],
2194+ helpString: `
2195+ <div>
2196+ Retrieves regular expression matches in the given text
2197+ </div>
2198+ <div>
2199+ Returns an array of groups (with the first group being the full match). If the regex contains the global flag (i.e. <code>/g</code>),
2200+ multiple nested arrays are returned for each match. If the regex is global, returns <code>[]</code> if no matches are found,
2201+ otherwise it returns null.
2202+ </div>
2203+ <div>
2204+ <strong>Example:</strong>
2205+ <pre>/let x color_green green lamp color_blue ||</pre>
2206+ <pre>/match pattern="green" {{var::x}} | /echo |/# [ "green" ] ||</pre>
2207+ <pre>/match pattern="color_(\\w+)" {{var::x}} | /echo |/# [ "color_green", "green" ] ||</pre>
2208+ <pre>/match pattern="/color_(\\w+)/g" {{var::x}} | /echo |/# [ [ "color_green", "green" ], [ "color_blue", "blue" ] ] ||</pre>
2209+ <pre>/match pattern="orange" {{var::x}} | /echo |/# null ||</pre>
2210+ <pre>/match pattern="/orange/g" {{var::x}} | /echo |/# [] ||</pre>
2211+ </div>
2212+ `,
2213+ }));
21342214
21352215 SlashCommandParser.addCommandObject(SlashCommand.fromProps({
21362216 name: 'chat-jump',