diff --git a/public/scripts/extensions/regex/engine.js b/public/scripts/extensions/regex/engine.js index ac7d9426c..65ea7981c 100644 --- a/public/scripts/extensions/regex/engine.js +++ b/public/scripts/extensions/regex/engine.js @@ -14,18 +14,22 @@ const regex_placement = { SENDAS: 4 } -// From: https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js +// Originally from: https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js function regexFromString(input) { - // Parse input - var m = input.match(/(\/?)(.+)\1([a-z]*)/i); - - // Invalid flags - if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) { - return RegExp(input); + try { + // Parse input + var m = input.match(/(\/?)(.+)\1([a-z]*)/i); + + // Invalid flags + if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) { + return RegExp(input); + } + + // Create the regular expression + return new RegExp(m[2], m[3]); + } catch { + return; } - - // Create the regular expression - return new RegExp(m[2], m[3]); } function getRegexedString(rawString, placement, { characterOverride } = {}) { @@ -52,6 +56,12 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { let match; let newString; const findRegex = regexFromString(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex); + + // The user skill issued. Return with nothing. + if (!findRegex) { + return; + } + while ((match = findRegex.exec(rawString)) !== null) { const fencedMatch = match[0]; const capturedMatch = match[1]; diff --git a/public/scripts/extensions/regex/index.js b/public/scripts/extensions/regex/index.js index e577437ba..2cb4c2b5b 100644 --- a/public/scripts/extensions/regex/index.js +++ b/public/scripts/extensions/regex/index.js @@ -1,4 +1,4 @@ -import { callPopup, eventSource, event_types, reloadCurrentChat, saveSettingsDebounced } from "../../../script.js"; +import { callPopup, eventSource, event_types, getCurrentChatId, reloadCurrentChat, saveSettingsDebounced } from "../../../script.js"; import { extension_settings } from "../../extensions.js"; import { uuidv4, waitUntilCondition } from "../../utils.js"; import { regex_placement } from "./engine.js"; @@ -50,7 +50,10 @@ async function saveRegexScript(regexScript, existingScriptIndex) { // Markdown is global, so reload the chat. if (regexScript.placement.includes(regex_placement.MD_DISPLAY)) { - await reloadCurrentChat(); + const currentChatId = getCurrentChatId(); + if (currentChatId !== undefined && currentChatId !== null) { + await reloadCurrentChat(); + } } }