Port #3031 onto new engine
| @@ -1,4 +1,4 @@ | |||
| 1 | import { characters, substituteParams, this_chid } from '../../../script.js'; | 1 | import { characters, substituteParams, substituteParamsExtended, this_chid } from '../../../script.js'; |
| 2 | import { extension_settings } from '../../extensions.js'; | 2 | import { extension_settings } from '../../extensions.js'; |
| 3 | import { regexFromString } from '../../utils.js'; | 3 | import { regexFromString } from '../../utils.js'; |
| 4 | export { | 4 | export { |
| @@ -22,6 +22,28 @@ const regex_placement = { | |||
| 22 | WORLD_INFO: 5, | 22 | WORLD_INFO: 5, |
| 23 | }; | 23 | }; |
| 24 | 24 | ||
| 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 | |||
| 25 | function getScopedRegex() { | 47 | function 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); |
| 27 | 49 | ||
| @@ -109,7 +131,10 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { | |||
| 109 | return newString; | 131 | return newString; |
| 110 | } | 132 | } |
| 111 | 133 | ||
| 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); | ||
| 113 | 138 | ||
| 114 | // The user skill issued. Return with nothing. | 139 | // The user skill issued. Return with nothing. |
| 115 | if (!findRegex) { | 140 | if (!findRegex) { |