Last active
October 1, 2025 13:27
-
-
Save Vocaned/ac2c2565d68fe865d0c1b0c11c9d1b3c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*: | |
| * @plugindesc [v1.0] Regex Text Replacer | |
| * @author voc | |
| * | |
| * @help | |
| * Replace any regex with any other text | |
| */ | |
| (function() { | |
| const DICTIONARY = [ | |
| [/\bfoot\b/, 'paw'], | |
| [/\bfeet\b/, 'paws'], | |
| ]; | |
| const patterns = {}; | |
| for (let i = 0; i < DICTIONARY.length; i++) { | |
| patterns[DICTIONARY[i][0].source] = DICTIONARY[i][1]; | |
| } | |
| const replacementRegex = new RegExp(Object.keys(patterns).map(p => `(${p})`).join('|'), 'gi'); | |
| function matchCase(text, replacement) { | |
| const isAllCaps = text.toUpperCase() === text; | |
| const isCapitalized = text.charAt(0).toUpperCase() === text.charAt(0) && text.slice(1).toLowerCase() === text.slice(1); | |
| const isAllLower = text.toLowerCase() === text; | |
| if (isAllCaps) { | |
| return replacement.toUpperCase(); | |
| } else if (isCapitalized) { | |
| return replacement.charAt(0).toUpperCase() + replacement.slice(1).toLowerCase(); | |
| } else if (isAllLower) { | |
| return replacement.toLowerCase(); | |
| } | |
| return replacement; | |
| } | |
| function replaceText(text) { | |
| if (typeof text !== "string") return text; | |
| return text.replace(replacementRegex, (...matches) => { | |
| for (let i = 1; i < matches.length - 2; i++) { | |
| if (matches[i] !== undefined) { | |
| changed = true; | |
| const newStr = patterns[Object.keys(patterns)[i - 1]]; | |
| if (newStr === newStr.toLowerCase()) return matchCase(matches[i], newStr); | |
| return newStr; | |
| } | |
| } | |
| }); | |
| } | |
| const _Window_Message_convertEscapeCharacters = Window_Message.prototype.convertEscapeCharacters; | |
| Window_Message.prototype.convertEscapeCharacters = function(text) { | |
| text = _Window_Message_convertEscapeCharacters.call(this, text); | |
| return replaceText(text); | |
| }; | |
| const _Window_Base_drawTextEx = Window_Base.prototype.drawTextEx; | |
| Window_Base.prototype.drawTextEx = function(text, x, y) { | |
| text = replaceText(text); | |
| return _Window_Base_drawTextEx.call(this, text, x, y); | |
| }; | |
| const _Window_Base_drawText = Window_Base.prototype.drawText; | |
| Window_Base.prototype.drawText = function(text, x, y, maxWidth, align) { | |
| text = replaceText(text); | |
| return _Window_Base_drawText.call(this, text, x, y, maxWidth, align); | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment