Untitled
// Function to perform multiple find-and-replace operations function multipleFindReplace(text, replacements) { let result = text; replacements.forEach(replacement => { const [find, replace] = replacement; // Create a regular expression to find all instances of the text const regex = new RegExp(find, 'g'); result = result.replace(regex, replace); }); return result; } // Example usage const text = "Hello World! Have a great World!"; const replacements = [ ["World", "Universe"], ["great", "wonderful"] ]; const newText = multipleFindReplace(text, replacements); console.log(newText); // Output: "Hello Universe! Have a wonderful Universe!"
Leave a Comment