Merge pull request #1641 from bdashore3/regex-upgrade

Regex: Updates
This commit is contained in:
Cohee 2024-01-11 02:56:41 +02:00 committed by GitHub
commit c1010edb37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 15 deletions

View File

@ -56,7 +56,7 @@
<div>
<textarea
class="regex_replace_string text_pole wide100p textarea_compact"
placeholder="Use {{match}} to include the matched text from the Find Regex"
placeholder="Use {{match}} to include the matched text from the Find Regex or $1, $2, etc. for capture groups."
rows="2"
></textarea>
</div>
@ -115,16 +115,16 @@
<input type="checkbox" name="run_on_edit" />
<span data-i18n="Run On Edit">Run On Edit</span>
</label>
<label class="checkbox flex-container">
<label class="checkbox flex-container" title="Substitute {{macros}} in Find Regex before running it">
<input type="checkbox" name="substitute_regex" />
<span data-i18n="Substitute Regex">Substitute Regex</span>
<span data-i18n="Substitute Regex">Substitute Regex (?)</span>
</label>
</div>
<div class="flex-container flexFlowColumn alignitemsstart">
<small>Replacement Strategy</small>
<select name="replace_strategy_select" class="margin0">
<option value="0">Replace</option>
<option value="1">Overlay</option>
<option value="1">Overlay (currently broken)</option>
</select>
</div>
</div>

View File

@ -109,19 +109,29 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
return newString;
}
newString = rawString.replace(findRegex, (fencedMatch) => {
let trimFencedMatch = filterString(fencedMatch, regexScript.trimStrings, { characterOverride });
// Run replacement. Currently does not support the Overlay strategy
newString = rawString.replace(findRegex, function(match) {
const args = [...arguments];
const replaceString = regexScript.replaceString.replace(/{{match}}/gi, '$0');
const replaceWithGroups = replaceString.replaceAll(/\$(\d)+/g, (_, num) => {
// Get a full match or a capture group
const match = args[Number(num)];
const subReplaceString = substituteRegexParams(
regexScript.replaceString,
trimFencedMatch,
{
characterOverride,
replaceStrategy: regexScript.replaceStrategy ?? regex_replace_strategy.REPLACE,
},
);
// No match found - return the empty string
if (!match) {
return '';
}
return subReplaceString;
// Remove trim strings from the match
const filteredMatch = filterString(match, regexScript.trimStrings, { characterOverride });
// TODO: Handle overlay here
return filteredMatch;
});
// Substitute at the end
return substituteParams(replaceWithGroups);
});
return newString;