diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index ae1523039..66555216c 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); @@ -2123,11 +2124,98 @@ export function initDefaultSlashCommands() {
/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 ||
+ true
if the match is found, false
otherwise.
+ /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 ||
+ /g
),
+ multiple nested arrays are returned for each match. If the regex is global, returns []
if no matches are found,
+ otherwise it returns an empty string.
+ /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 |/# ||
+ /match pattern="/orange/g" {{var::x}} | /echo |/# [] ||