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> <div>
<textarea <textarea
class="regex_replace_string text_pole wide100p textarea_compact" 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" rows="2"
></textarea> ></textarea>
</div> </div>
@ -115,16 +115,16 @@
<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"> <label class="checkbox flex-container" title="Substitute {{macros}} in Find Regex before running it">
<input type="checkbox" name="substitute_regex" /> <input type="checkbox" name="substitute_regex" />
<span data-i18n="Substitute Regex">Substitute Regex</span> <span data-i18n="Substitute Regex">Substitute Regex (?)</span>
</label> </label>
</div> </div>
<div class="flex-container flexFlowColumn alignitemsstart"> <div class="flex-container flexFlowColumn alignitemsstart">
<small>Replacement Strategy</small> <small>Replacement Strategy</small>
<select name="replace_strategy_select" class="margin0"> <select name="replace_strategy_select" class="margin0">
<option value="0">Replace</option> <option value="0">Replace</option>
<option value="1">Overlay</option> <option value="1">Overlay (currently broken)</option>
</select> </select>
</div> </div>
</div> </div>

View File

@ -109,19 +109,29 @@ 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}}/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( // No match found - return the empty string
regexScript.replaceString, if (!match) {
trimFencedMatch, return '';
{ }
characterOverride,
replaceStrategy: regexScript.replaceStrategy ?? regex_replace_strategy.REPLACE,
},
);
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; return newString;