mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Remove regex replace strategy selector
This commit is contained in:
@ -19,14 +19,6 @@ const regex_placement = {
|
||||
SLASH_COMMAND: 3,
|
||||
};
|
||||
|
||||
/**
|
||||
* @enum {number} How the regex script should replace the matched string
|
||||
*/
|
||||
const regex_replace_strategy = {
|
||||
REPLACE: 0,
|
||||
OVERLAY: 1,
|
||||
};
|
||||
|
||||
/**
|
||||
* Instantiates a regular expression from a string.
|
||||
* @param {string} input The input string.
|
||||
@ -153,86 +145,3 @@ function filterString(rawString, trimStrings, { characterOverride } = {}) {
|
||||
|
||||
return finalString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitutes regex-specific and normal parameters
|
||||
* @param {string} rawString
|
||||
* @param {string} regexMatch
|
||||
* @param {RegexSubstituteParams} params The parameters to use for the regex substitution
|
||||
* @returns {string} The substituted string
|
||||
* @typedef {{characterOverride?: string, replaceStrategy?: number}} RegexSubstituteParams The parameters to use for the regex substitution
|
||||
*/
|
||||
function substituteRegexParams(rawString, regexMatch, { characterOverride, replaceStrategy } = {}) {
|
||||
let finalString = rawString;
|
||||
finalString = substituteParams(finalString, undefined, characterOverride);
|
||||
|
||||
let overlaidMatch = regexMatch;
|
||||
// TODO: Maybe move the for loops into a separate function?
|
||||
if (replaceStrategy === regex_replace_strategy.OVERLAY) {
|
||||
const splitReplace = finalString.split('{{match}}');
|
||||
|
||||
// There's a prefix
|
||||
if (splitReplace[0]) {
|
||||
// Fetch the prefix
|
||||
const splicedPrefix = spliceSymbols(splitReplace[0], false);
|
||||
|
||||
// Sequentially remove all occurrences of prefix from start of split
|
||||
const splitMatch = overlaidMatch.split(splicedPrefix);
|
||||
let sliceNum = 0;
|
||||
for (let index = 0; index < splitMatch.length; index++) {
|
||||
if (splitMatch[index].length === 0) {
|
||||
sliceNum++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
overlaidMatch = splitMatch.slice(sliceNum, splitMatch.length).join(splicedPrefix);
|
||||
}
|
||||
|
||||
// There's a suffix
|
||||
if (splitReplace[1]) {
|
||||
// Fetch the suffix
|
||||
const splicedSuffix = spliceSymbols(splitReplace[1], true);
|
||||
|
||||
// Sequential removal of all suffix occurrences from end of split
|
||||
const splitMatch = overlaidMatch.split(splicedSuffix);
|
||||
let sliceNum = 0;
|
||||
for (let index = splitMatch.length - 1; index >= 0; index--) {
|
||||
if (splitMatch[index].length === 0) {
|
||||
sliceNum++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
overlaidMatch = splitMatch.slice(0, splitMatch.length - sliceNum).join(splicedSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
// Only one match is replaced. This is by design
|
||||
finalString = finalString.replace('{{match}}', overlaidMatch) || finalString.replace('{{match}}', regexMatch);
|
||||
|
||||
return finalString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Splices common sentence symbols and whitespace from the beginning and end of a string.
|
||||
* Using a for loop due to sequential ordering.
|
||||
* @param {string} rawString The raw string to splice
|
||||
* @param {boolean} isSuffix String is a suffix
|
||||
* @returns {string} The spliced string
|
||||
*/
|
||||
function spliceSymbols(rawString, isSuffix) {
|
||||
let offset = 0;
|
||||
|
||||
for (const ch of isSuffix ? rawString.split('').reverse() : rawString) {
|
||||
if (ch.match(/[^\w.,?'!]/)) {
|
||||
offset++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return isSuffix ? rawString.substring(0, rawString.length - offset) : rawString.substring(offset);
|
||||
}
|
||||
|
Reference in New Issue
Block a user