Skip to content

Commit 6cb1eb3

Browse files
committed
Change return to empty string on no single match
1 parent 0cd0ce2 commit 6cb1eb3

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

public/scripts/slash-commands.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,14 +2177,15 @@ export function initDefaultSlashCommands() {
21772177
if (!pattern) {
21782178
throw new Error('Argument of \'pattern=\' cannot be empty');
21792179
}
2180-
let re = regexFromString(pattern.toString());
2180+
const re = regexFromString(pattern.toString());
21812181
if (!re) {
21822182
throw new Error('The value of \'pattern\' argument is not a valid regular expression.');
21832183
}
21842184
if (re.flags.includes('g')) {
21852185
return JSON.stringify([...text.toString().matchAll(re)]);
21862186
} else {
2187-
return JSON.stringify(text.toString().match(re));
2187+
const match = text.toString().match(re);
2188+
return match ? JSON.stringify(match) : '';
21882189
}
21892190
}),
21902191
returns: 'group array for each match',
@@ -2205,15 +2206,15 @@ export function initDefaultSlashCommands() {
22052206
<div>
22062207
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>),
22072208
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.
22092210
</div>
22102211
<div>
22112212
<strong>Example:</strong>
22122213
<pre><code class="language-stscript">/let x color_green green lamp color_blue ||</code></pre>
22132214
<pre><code class="language-stscript">/match pattern="green" {{var::x}} | /echo |/# [ "green" ] ||</code></pre>
22142215
<pre><code class="language-stscript">/match pattern="color_(\\w+)" {{var::x}} | /echo |/# [ "color_green", "green" ] ||</code></pre>
22152216
<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>
22172218
<pre><code class="language-stscript">/match pattern="/orange/g" {{var::x}} | /echo |/# [] ||</code></pre>
22182219
</div>
22192220
`,

0 commit comments

Comments
 (0)