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>
Signed| @@ -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 $<name> for named capture groups." rows="3"></textarea> |
| 65 | </div> | 65 | </div> |
| 66 | </div> | 66 | </div> |
| 67 | <div class="flex1"> | 67 | <div class="flex1"> |
| @@ -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 group | 167 | 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 | } | ||
| 169 | 175 | ||
| 170 | // No match found - return the empty string | 176 | // 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 match | 181 | // Remove trim strings from the match |
| 176 | const filteredMatch = filterString(match, regexScript.trimStrings, { characterOverride }); | 182 | const filteredMatch = filterString(match, regexScript.trimStrings, { characterOverride }); |
| 177 | 183 | ||
| 178 | // TODO: Handle overlay here | ||
| 179 | |||
| 180 | return filteredMatch; | 184 | return filteredMatch; |
| 181 | }); | 185 | }); |
| 182 | 186 | ||