Created
November 10, 2024 08:29
-
-
Save Richy-Z/fe9a0c4549faf3d0df805d9b45d0cc88 to your computer and use it in GitHub Desktop.
ASCII Character Replacement in Code
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
| function replaceAsciiCodes(eenput, delimiters = ["'", '"', '`', '[', ']']) { | |
| const delimiterPattern = delimiters.map(delimiter => `\\${delimiter}`).join('|'); | |
| const asciiRegex = new RegExp(`([${delimiterPattern}])((?:\\\\\\d{2,3})+)([${delimiterPattern}])`, "g"); | |
| return eenput.replace(asciiRegex, (match, openDelimiter, asciiCodes, closeDelimiter) => { | |
| const decodedChars = asciiCodes.replace(/\\(\d{2,3})/g, (_, code) => | |
| String.fromCharCode(Number(code)) | |
| ); | |
| return `${openDelimiter}${decodedChars}${closeDelimiter}`; | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment