-3

How do you replace specific words from a string? If I want to replace ["hi", "hello", "welcome"] with greetings in this string. (Case-insensitive)

So Hi there Welcome hi would become greetings there greetings greetings.

eisbehr
  • 12,243
  • 7
  • 38
  • 63
Tim
  • 155
  • 1
  • 11

1 Answers1

0

Follow the Example

String.prototype.allReplace = function(obj) {
    var retStr = this;
    for (var x in obj) {
        retStr = retStr.replace(new RegExp(x, 'g'), obj[x]);
    }
    return retStr;
};
var v = 'aabbaabbcc'.allReplace({'a': 'h', 'b': 'o'});  
Faiz Akram
  • 559
  • 4
  • 10
  • Yes, but that would work if I did it with one word at a time. I want to be able to do it with a big array that could change. – Tim Aug 08 '18 at 09:14