| 1998 | 1998 | return ''; |
| 1999 | 1999 | }, |
| 2000 | 2000 | })); |
| 2001 | + SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 2002 | + name: 'replace', |
| 2003 | + aliases: ['re'], |
| 2004 | + callback: (async ({mode = 'literal', pattern, replacer = ''}, text) => { |
| 2005 | + if (pattern === '') |
| 2006 | + throw new Error("Argument of 'pattern=' cannot be empty"); |
| 2007 | + switch (mode) { |
| 2008 | + case 'literal': |
| 2009 | + return text.replaceAll(pattern, replacer); |
| 2010 | + case 'regex': |
| 2011 | + return text.replace(regexFromString(pattern), replacer); |
| 2012 | + default: |
| 2013 | + throw new Error("Invalid '/replace mode=' argument specified!"); |
| 2014 | + } |
| 2015 | + }), |
| 2016 | + returns: 'replaced text', |
| 2017 | + namedArgumentList: [ |
| 2018 | + SlashCommandNamedArgument.fromProps({ |
| 2019 | + name: 'mode', |
| 2020 | + description: 'Replaces occurrence(s) of a pattern', |
| 2021 | + typeList: [ARGUMENT_TYPE.STRING], |
| 2022 | + defaultValue: 'literal', |
| 2023 | + enumList: ['literal', 'regex'], |
| 2024 | + }), |
| 2025 | + new SlashCommandNamedArgument( |
| 2026 | + 'pattern', 'pattern to search with', [ARGUMENT_TYPE.STRING], true, false, |
| 2027 | + ), |
| 2028 | + new SlashCommandNamedArgument( |
| 2029 | + 'replacer', 'replacement text for matches', [ARGUMENT_TYPE.STRING], false, false, '', |
| 2030 | + ), |
| 2031 | + ], |
| 2032 | + unnamedArgumentList: [ |
| 2033 | + new SlashCommandArgument( |
| 2034 | + 'text to affect', [ARGUMENT_TYPE.STRING], true, false, |
| 2035 | + ), |
| 2036 | + ], |
| 2037 | + helpString: ` |
| 2038 | + <div> |
| 2039 | + Replaces text within the provided string based on the pattern. |
| 2040 | + </div> |
| 2041 | + <div> |
| 2042 | + If <code>mode</code> is <code>literal</code> (or omitted), <code>pattern</code> is a literal search string (case-sensitive).<br /> |
| 2043 | + If <code>mode</code> is <code>regex</code>, <code>pattern</code> is parsed as an ECMAScript Regular Expression.<br /> |
| 2044 | + The <code>replacer</code> replaces based on the <code>pattern</code> in the input text.<br /> |
| 2045 | + If <code>replacer</code> is omitted, the replacement(s) will be an empty string.<br /> |
| 2046 | + </div> |
| 2047 | + <div> |
| 2048 | + <strong>Example:</strong> |
| 2049 | + <pre>/let x Blue house and blue car || </pre> |
| 2050 | + <pre>/replace pattern="blue" {{var::x}} | /echo |/# Blue house and car ||</pre> |
| 2051 | + <pre>/replace pattern="blue" replacer="red" {{var::x}} | /echo |/# Blue house and red car ||</pre> |
| 2052 | + <pre>/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}} | /echo |/# red house and blue car ||</pre> |
| 2053 | + <pre>/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}} | /echo |/# red house and red car ||</pre> |
| 2054 | + </div> |
| 2055 | + `, |
| 2056 | + })); |
| 2001 | 2057 | |
| 2002 | 2058 | registerVariableCommands(); |
| 2003 | 2059 | } |