Add support for named regex capture groups. (#4397) * Update UpdateAndStart.bat * regex named capture group * Add row to replace with textarea * Move checkbox up * Revert toggle and syntax preferences * Remove comment --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

fd00b5a5b8e9536349661b69fe46915cf8372b68

zengls <94688592+zeroDtree@users.noreply.github.com>

Signed
2 files changed, +10 -6Showing whitespace changes
public/scripts/extensions/regex/editor.html+1 -1
@@ -61,7 +61,7 @@
61 <small data-i18n="Replace With">Replace With</small>61 <small data-i18n="Replace With">Replace With</small>
62 </label>62 </label>
63 <div>63 <div>
64 <textarea class="regex_replace_string text_pole wide100p textarea_compact" data-i18n="[placeholder]ext_regex_replace_string_placeholder" placeholder="Use {{match}} to include the matched text from the Find Regex or $1, $2, etc. for capture groups." rows="2"></textarea>64 <textarea class="regex_replace_string text_pole wide100p textarea_compact" data-i18n="[placeholder]ext_regex_replace_string_placeholder" placeholder="Use {{match}} to include the matched text from the Find Regex, $1, $2, etc. for numbered capture groups, or $&lt;name&gt; for named capture groups." rows="3"></textarea>
65 </div>65 </div>
66 </div>66 </div>
67 <div class="flex1">67 <div class="flex1">
public/scripts/extensions/regex/engine.js+9 -5
@@ -163,9 +163,15 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
163 newString = rawString.replace(findRegex, function (match) {163 newString = rawString.replace(findRegex, function (match) {
164 const args = [...arguments];164 const args = [...arguments];
165 const replaceString = regexScript.replaceString.replace(/{{match}}/gi, '$0');165 const replaceString = regexScript.replaceString.replace(/{{match}}/gi, '$0');
166 const replaceWithGroups = replaceString.replaceAll(/\$(\d+)/g, (_, num) => {166 const replaceWithGroups = replaceString.replaceAll(/\$(\d+)|\$<([^>]+)>/g, (_, num, groupName) => {
167 // Get a full match or a capture group167 if (num) {
168 const match = args[Number(num)];168 // Handle numbered capture groups ($1, $2, etc.)
169 match = args[Number(num)];
170 } else if (groupName) {
171 // Handle named capture groups ($<name>)
172 const groups = args[args.length - 1];
173 match = groups && typeof groups === 'object' && groups[groupName];
174 }
169175
170 // No match found - return the empty string176 // No match found - return the empty string
171 if (!match) {177 if (!match) {
@@ -175,8 +181,6 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
175 // Remove trim strings from the match181 // Remove trim strings from the match
176 const filteredMatch = filterString(match, regexScript.trimStrings, { characterOverride });182 const filteredMatch = filterString(match, regexScript.trimStrings, { characterOverride });
177183
178 // TODO: Handle overlay here
179
180 return filteredMatch;184 return filteredMatch;
181 });185 });
182186