mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Regex: Add substitution to regex and undefined checks
Sometimes a user may want to substitute variables in the regex itself rather than just matching those variables. This can be optionally enabled in the editor. In addition, try preventing crashes by checking for undefined variables or null coalescing. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@@ -89,6 +89,10 @@
|
|||||||
<input type="checkbox" name="run_on_edit" />
|
<input type="checkbox" name="run_on_edit" />
|
||||||
<span data-i18n="Run On Edit">Run On Edit</span>
|
<span data-i18n="Run On Edit">Run On Edit</span>
|
||||||
</label>
|
</label>
|
||||||
|
<label class="checkbox flex-container">
|
||||||
|
<input type="checkbox" name="substitute_regex" />
|
||||||
|
<span data-i18n="Substitute Regex">Substitute Regex</span>
|
||||||
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -45,13 +45,14 @@ function getRegexedString(rawString, placement) {
|
|||||||
|
|
||||||
// Runs the provided regex script on the given string
|
// Runs the provided regex script on the given string
|
||||||
function runRegexScript(regexScript, rawString) {
|
function runRegexScript(regexScript, rawString) {
|
||||||
if (!!(regexScript.disabled)) {
|
if (!regexScript || !!(regexScript.disabled) || !regexScript?.findRegex || !rawString) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let match;
|
let match;
|
||||||
let newString;
|
let newString;
|
||||||
const findRegex = regexFromString(regexScript.findRegex);
|
console.log(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex)
|
||||||
|
const findRegex = regexFromString(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex);
|
||||||
while ((match = findRegex.exec(rawString)) !== null) {
|
while ((match = findRegex.exec(rawString)) !== null) {
|
||||||
const fencedMatch = match[0];
|
const fencedMatch = match[0];
|
||||||
const capturedMatch = match[1];
|
const capturedMatch = match[1];
|
||||||
|
@@ -97,16 +97,25 @@ async function onRegexEditorOpenClick(existingId) {
|
|||||||
existingScriptIndex = extension_settings.regex.findIndex((script) => script.scriptName === existingScriptName);
|
existingScriptIndex = extension_settings.regex.findIndex((script) => script.scriptName === existingScriptName);
|
||||||
if (existingScriptIndex !== -1) {
|
if (existingScriptIndex !== -1) {
|
||||||
const existingScript = extension_settings.regex[existingScriptIndex];
|
const existingScript = extension_settings.regex[existingScriptIndex];
|
||||||
editorHtml.find(`.regex_script_name`).val(existingScript.scriptName);
|
if (existingScript.scriptName) {
|
||||||
editorHtml.find(`.find_regex`).val(existingScript.findRegex);
|
editorHtml.find(`.regex_script_name`).val(existingScript.scriptName);
|
||||||
editorHtml.find(`.regex_replace_string`).val(existingScript.replaceString);
|
} else {
|
||||||
|
toastr.error("This script doesn't have a name! Please delete it.")
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
editorHtml.find(`.find_regex`).val(existingScript.findRegex || "");
|
||||||
|
editorHtml.find(`.regex_replace_string`).val(existingScript.replaceString || "");
|
||||||
editorHtml.find(`.regex_trim_strings`).val(existingScript.trimStrings?.join("\n") || []);
|
editorHtml.find(`.regex_trim_strings`).val(existingScript.trimStrings?.join("\n") || []);
|
||||||
editorHtml
|
editorHtml
|
||||||
.find(`input[name="disabled"]`)
|
.find(`input[name="disabled"]`)
|
||||||
.prop("checked", existingScript.disabled ?? false);
|
.prop("checked", existingScript.disabled ?? false);
|
||||||
editorHtml
|
editorHtml
|
||||||
.find(`input[name="run_on_edit"]`)
|
.find(`input[name="run_on_edit"]`)
|
||||||
.prop("checked", existingScript.runOnEdit);
|
.prop("checked", existingScript.runOnEdit ?? false);
|
||||||
|
editorHtml
|
||||||
|
.find(`input[name="substitute_regex"]`)
|
||||||
|
.prop("checked", existingScript.substituteRegex ?? false);
|
||||||
|
|
||||||
existingScript.placement.forEach((element) => {
|
existingScript.placement.forEach((element) => {
|
||||||
editorHtml
|
editorHtml
|
||||||
@@ -145,6 +154,10 @@ async function onRegexEditorOpenClick(existingId) {
|
|||||||
runOnEdit:
|
runOnEdit:
|
||||||
editorHtml
|
editorHtml
|
||||||
.find(`input[name="run_on_edit"]`)
|
.find(`input[name="run_on_edit"]`)
|
||||||
|
.prop("checked"),
|
||||||
|
substituteRegex:
|
||||||
|
editorHtml
|
||||||
|
.find(`input[name="substitute_regex"]`)
|
||||||
.prop("checked")
|
.prop("checked")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user