Merge pull request #3928 from BismuthGlass/feature/regex-test-match Add both `test` and `match` regex commands

a5d63b064ad694f681cceda3348df45f8e8c60d3

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +94 -6Showing whitespace changes
public/scripts/slash-commands.js+94 -6
@@ -2079,8 +2079,9 @@ export function initDefaultSlashCommands() {
20792079 name: 'replace',
20802080 aliases: ['re'],
20812081 callback: (async ({ mode = 'literal', pattern, replacer = '' }, text) => {
20822082 if (!pattern === '') {
20832083 throw new Error('Argument of \'pattern=\' cannot be empty');
2084+ }
20842085 switch (mode) {
20852086 case 'literal':
20862087 return text.replaceAll(pattern, replacer);
@@ -2123,11 +2124,98 @@ export function initDefaultSlashCommands() {
21232124 </div>
21242125 <div>
21252126 <strong>Example:</strong>
21262127 <pre><code class="language-stscript">/let x Blue house and blue car || </code></pre>
21272128 <pre><code class="language-stscript">/replace pattern="blue" {{var::x}} | /echo |/# Blue house and car ||</code></pre>
21282129 <pre><code class="language-stscript">/replace pattern="blue" replacer="red" {{var::x}} | /echo |/# Blue house and red car ||</code></pre>
21292130 <pre><code class="language-stscript">/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}} | /echo |/# red house and blue car ||</code></pre>
21302131 <pre><code class="language-stscript">/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}} | /echo |/# red house and red car ||</code></pre>
2132+ </div>
2133+ `,
2134+ }));
2135+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2136+ name: 'test',
2137+ callback: (({ pattern }, text) => {
2138+ if (!pattern) {
2139+ throw new Error('Argument of \'pattern=\' cannot be empty');
2140+ }
2141+ const re = regexFromString(pattern.toString());
2142+ if (!re) {
2143+ throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
2144+ }
2145+ return JSON.stringify(re.test(text.toString()));
2146+ }),
2147+ returns: 'true | false',
2148+ namedArgumentList: [
2149+ new SlashCommandNamedArgument(
2150+ 'pattern', 'pattern to find', [ARGUMENT_TYPE.STRING], true, false,
2151+ ),
2152+ ],
2153+ unnamedArgumentList: [
2154+ new SlashCommandArgument(
2155+ 'text to test', [ARGUMENT_TYPE.STRING], true, false,
2156+ ),
2157+ ],
2158+ helpString: `
2159+ <div>
2160+ Tests text for a regular expression match.
2161+ </div>
2162+ <div>
2163+ Returns <code>true</code> if the match is found, <code>false</code> otherwise.
2164+ </div>
2165+ <div>
2166+ <strong>Example:</strong>
2167+ <pre><code class="language-stscript">/let x Blue house and green car ||</code></pre>
2168+ <pre><code class="language-stscript">/test pattern="green" {{var::x}} | /echo |/# true ||</code></pre>
2169+ <pre><code class="language-stscript">/test pattern="blue" {{var::x}} | /echo |/# false ||</code></pre>
2170+ <pre><code class="language-stscript">/test pattern="/blue/i" {{var::x}} | /echo |/# true ||</code></pre>
2171+ </div>
2172+ `,
2173+ }));
2174+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2175+ name: 'match',
2176+ callback: (({ pattern }, text) => {
2177+ if (!pattern) {
2178+ throw new Error('Argument of \'pattern=\' cannot be empty');
2179+ }
2180+ const re = regexFromString(pattern.toString());
2181+ if (!re) {
2182+ throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
2183+ }
2184+ if (re.flags.includes('g')) {
2185+ return JSON.stringify([...text.toString().matchAll(re)]);
2186+ } else {
2187+ const match = text.toString().match(re);
2188+ return match ? JSON.stringify(match) : '';
2189+ }
2190+ }),
2191+ returns: 'group array for each match',
2192+ namedArgumentList: [
2193+ new SlashCommandNamedArgument(
2194+ 'pattern', 'pattern to find', [ARGUMENT_TYPE.STRING], true, false,
2195+ ),
2196+ ],
2197+ unnamedArgumentList: [
2198+ new SlashCommandArgument(
2199+ 'text to match against', [ARGUMENT_TYPE.STRING], true, false,
2200+ ),
2201+ ],
2202+ helpString: `
2203+ <div>
2204+ Retrieves regular expression matches in the given text
2205+ </div>
2206+ <div>
2207+ 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>),
2208+ multiple nested arrays are returned for each match. If the regex is global, returns <code>[]</code> if no matches are found,
2209+ otherwise it returns an empty string.
2210+ </div>
2211+ <div>
2212+ <strong>Example:</strong>
2213+ <pre><code class="language-stscript">/let x color_green green lamp color_blue ||</code></pre>
2214+ <pre><code class="language-stscript">/match pattern="green" {{var::x}} | /echo |/# [ "green" ] ||</code></pre>
2215+ <pre><code class="language-stscript">/match pattern="color_(\\w+)" {{var::x}} | /echo |/# [ "color_green", "green" ] ||</code></pre>
2216+ <pre><code class="language-stscript">/match pattern="/color_(\\w+)/g" {{var::x}} | /echo |/# [ [ "color_green", "green" ], [ "color_blue", "blue" ] ] ||</code></pre>
2217+ <pre><code class="language-stscript">/match pattern="orange" {{var::x}} | /echo |/# ||</code></pre>
2218+ <pre><code class="language-stscript">/match pattern="/orange/g" {{var::x}} | /echo |/# [] ||</code></pre>
21312219 </div>
21322220 `,
21332221 }));