Skip to content

Instantly share code, notes, and snippets.

@TommyZG
Created October 16, 2023 08:33
Show Gist options
  • Save TommyZG/e3d8a2594c9ad60a3443d85468433d04 to your computer and use it in GitHub Desktop.
Save TommyZG/e3d8a2594c9ad60a3443d85468433d04 to your computer and use it in GitHub Desktop.
Function to replace words/substrings in text, multiple lookups possible
// Function to replace substrings
function replaceText(input) {
// Define a map of substrings to their replacements
const dictionary = {
'fren': 'friend',
'ngmi': 'wagmi',
'rekt': 'screwed',
'rug': 'sbf'
};
// Create a regular expression pattern to match any of the specified substrings
const pattern = new RegExp(Object.keys(dictionary).join('|'), 'g');
// Use the replace method to replace matched substrings with their replacements
const replacedText = input.replace(pattern, match => dictionary[match]);
return replacedText;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment