Port #3031 onto new engine

48d8e6e2c385218e64926f2fd72dd84943f67416

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

1 files changed, +27 -2Ignore whitespace
public/scripts/extensions/regex/engine.js+27 -2
@@ -1,4 +1,4 @@
11import { characters, substituteParams, substituteParamsExtended, this_chid } from '../../../script.js';
22import { extension_settings } from '../../extensions.js';
33import { regexFromString } from '../../utils.js';
44export {
@@ -22,6 +22,28 @@ const regex_placement = {
2222 WORLD_INFO: 5,
2323};
2424
25+function sanitizeRegexMacro(x) {
26+ return (x && typeof x === 'string') ?
27+ x.replaceAll(/[\n\r\t\v\f\0.^$*+?{}[\]\\/|()]/gs, function (s) {
28+ switch (s) {
29+ case '\n':
30+ return '\\n';
31+ case '\r':
32+ return '\\r';
33+ case '\t':
34+ return '\\t';
35+ case '\v':
36+ return '\\v';
37+ case '\f':
38+ return '\\f';
39+ case '\0':
40+ return '\\0';
41+ default:
42+ return '\\' + s;
43+ }
44+ }) : x;
45+}
46+
2547function getScopedRegex() {
2648 const isAllowed = extension_settings?.character_allowed_regex?.includes(characters?.[this_chid]?.avatar);
2749
@@ -109,7 +131,10 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
109131 return newString;
110132 }
111133
112- const findRegex = regexFromString(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex);
134+ const regexString = regexScript.substituteRegex
135+ ? substituteParamsExtended(regexScript.findRegex, {}, sanitizeRegexMacro)
136+ : regexScript.findRegex;
137+ const findRegex = regexFromString(regexString);
113138
114139 // The user skill issued. Return with nothing.
115140 if (!findRegex) {