0

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";
    }
}
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
Alkalyn
  • 1
  • 1
  • Sure, can you please help us with what you have tried so far? Even if that's in the early stages, you can paste your code in the OP (edit and paste) and we can help to make that code work – Tushar Gupta Mar 07 '22 at 01:23
  • A [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) is one good way to store mappings from one string to another. – jarmod Mar 07 '22 at 01:31

1 Answers1

0

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.

Jackson H
  • 171
  • 10