I need help in making this function be able to replace not only a single word ("apple" to "orange"), but a whole sentence too ("eat apple" to "sleep well"), both for regular and UPPERCASE. I've tried this :
const re = /eat_apple/gi;
const str = "eat apple";
const replacer = (match) => {
if(match === "eat apple") {
return "sleep well";
}
else if(match === "EAT APPLE") {
return "SLEEP WELL";
}
else {
return "eat apple";
}
}
Check out case-insensitive string comparison. You can check the lowercased string, then do the replacement.
If you need the capitalization to somehow match the capitalization as it was originally, specify that by editing your question.