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 @@
1import { characters, substituteParams, this_chid } from '../../../script.js';1import { characters, substituteParams, substituteParamsExtended, this_chid } from '../../../script.js';
2import { extension_settings } from '../../extensions.js';2import { extension_settings } from '../../extensions.js';
3import { regexFromString } from '../../utils.js';3import { regexFromString } from '../../utils.js';
4export {4export {
@@ -22,6 +22,28 @@ const regex_placement = {
22 WORLD_INFO: 5,22 WORLD_INFO: 5,
23};23};
2424
25function 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
25function getScopedRegex() {47function getScopedRegex() {
26 const isAllowed = extension_settings?.character_allowed_regex?.includes(characters?.[this_chid]?.avatar);48 const isAllowed = extension_settings?.character_allowed_regex?.includes(characters?.[this_chid]?.avatar);
2749
@@ -109,7 +131,10 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
109 return newString;131 return newString;
110 }132 }
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
114 // The user skill issued. Return with nothing.139 // The user skill issued. Return with nothing.
115 if (!findRegex) {140 if (!findRegex) {