mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Regex: Update to capture group logic
Currently doesn't support the overlay method for regex scripts. That will have to be added in a future commit. People can refer to capture groups using $1, $2, etc. {{match}} gets aliased to $1. Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
@ -109,19 +109,22 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
|
|||||||
return newString;
|
return newString;
|
||||||
}
|
}
|
||||||
|
|
||||||
newString = rawString.replace(findRegex, (fencedMatch) => {
|
// Run replacement. Currently does not support the Overlay strategy
|
||||||
let trimFencedMatch = filterString(fencedMatch, regexScript.trimStrings, { characterOverride });
|
newString = rawString.replace(findRegex, function(match) {
|
||||||
|
const args = [...arguments];
|
||||||
|
const replaceString = regexScript.replaceString.replace('{{match}}', '$1');
|
||||||
|
const replaceWithGroups = replaceString.replaceAll(/\$(\d)+/g, (_, num) => {
|
||||||
|
// match is found here
|
||||||
|
const match = args[Number(num)];
|
||||||
|
const filteredMatch = filterString(match, regexScript.trimStrings, { characterOverride });
|
||||||
|
|
||||||
const subReplaceString = substituteRegexParams(
|
// TODO: Handle overlay here
|
||||||
regexScript.replaceString,
|
|
||||||
trimFencedMatch,
|
|
||||||
{
|
|
||||||
characterOverride,
|
|
||||||
replaceStrategy: regexScript.replaceStrategy ?? regex_replace_strategy.REPLACE,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return subReplaceString;
|
return filteredMatch;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Substitute at the end
|
||||||
|
return substituteParams(replaceWithGroups);
|
||||||
});
|
});
|
||||||
|
|
||||||
return newString;
|
return newString;
|
||||||
@ -136,10 +139,10 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
|
|||||||
*/
|
*/
|
||||||
function filterString(rawString, trimStrings, { characterOverride } = {}) {
|
function filterString(rawString, trimStrings, { characterOverride } = {}) {
|
||||||
let finalString = rawString;
|
let finalString = rawString;
|
||||||
trimStrings.forEach((trimString) => {
|
for (const trimString of trimStrings) {
|
||||||
const subTrimString = substituteParams(trimString, undefined, characterOverride);
|
const subTrimString = substituteParams(trimString, undefined, characterOverride);
|
||||||
finalString = finalString.replaceAll(subTrimString, '');
|
finalString = finalString.replaceAll(subTrimString, '');
|
||||||
});
|
}
|
||||||
|
|
||||||
return finalString;
|
return finalString;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user