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
.
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
.
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'});