Change return to empty string on no single match

6cb1eb3fe69cfebd2c71138c6b0971007007acc4

Crow <bismuthglass@protonmail.com>

1 files changed, +5 -4Showing whitespace changes
public/scripts/slash-commands.js+5 -4
@@ -2177,14 +2177,15 @@ export function initDefaultSlashCommands() {
2177 if (!pattern) {2177 if (!pattern) {
2178 throw new Error('Argument of \'pattern=\' cannot be empty');2178 throw new Error('Argument of \'pattern=\' cannot be empty');
2179 }2179 }
2180 let re = regexFromString(pattern.toString());2180 const re = regexFromString(pattern.toString());
2181 if (!re) {2181 if (!re) {
2182 throw new Error('The value of \'pattern\' argument is not a valid regular expression.');2182 throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
2183 }2183 }
2184 if (re.flags.includes('g')) {2184 if (re.flags.includes('g')) {
2185 return JSON.stringify([...text.toString().matchAll(re)]);2185 return JSON.stringify([...text.toString().matchAll(re)]);
2186 } else {2186 } else {
2187 return JSON.stringify(text.toString().match(re));2187 const match = text.toString().match(re);
2188 return match ? JSON.stringify(match) : '';
2188 }2189 }
2189 }),2190 }),
2190 returns: 'group array for each match',2191 returns: 'group array for each match',
@@ -2205,7 +2206,7 @@ export function initDefaultSlashCommands() {
2205 <div>2206 <div>
2206 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>),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>),
2207 multiple nested arrays are returned for each match. If the regex is global, returns <code>[]</code> if no matches are found,2208 multiple nested arrays are returned for each match. If the regex is global, returns <code>[]</code> if no matches are found,
2208 otherwise it returns null.2209 otherwise it returns an empty string.
2209 </div>2210 </div>
2210 <div>2211 <div>
2211 <strong>Example:</strong>2212 <strong>Example:</strong>
@@ -2213,7 +2214,7 @@ export function initDefaultSlashCommands() {
2213 <pre><code class="language-stscript">/match pattern="green" {{var::x}} | /echo |/# [ "green" ] ||</code></pre>2214 <pre><code class="language-stscript">/match pattern="green" {{var::x}} | /echo |/# [ "green" ] ||</code></pre>
2214 <pre><code class="language-stscript">/match pattern="color_(\\w+)" {{var::x}} | /echo |/# [ "color_green", "green" ] ||</code></pre>2215 <pre><code class="language-stscript">/match pattern="color_(\\w+)" {{var::x}} | /echo |/# [ "color_green", "green" ] ||</code></pre>
2215 <pre><code class="language-stscript">/match pattern="/color_(\\w+)/g" {{var::x}} | /echo |/# [ [ "color_green", "green" ], [ "color_blue", "blue" ] ] ||</code></pre>2216 <pre><code class="language-stscript">/match pattern="/color_(\\w+)/g" {{var::x}} | /echo |/# [ [ "color_green", "green" ], [ "color_blue", "blue" ] ] ||</code></pre>
2216 <pre><code class="language-stscript">/match pattern="orange" {{var::x}} | /echo |/# null ||</code></pre>2217 <pre><code class="language-stscript">/match pattern="orange" {{var::x}} | /echo |/# ||</code></pre>
2217 <pre><code class="language-stscript">/match pattern="/orange/g" {{var::x}} | /echo |/# [] ||</code></pre>2218 <pre><code class="language-stscript">/match pattern="/orange/g" {{var::x}} | /echo |/# [] ||</code></pre>
2218 </div>2219 </div>
2219 `,2220 `,