let string = 'This is cool!'
const words = ['cool', 'this']
words.map(word => {
string = this.string.replace(word, `<a href="#">${word}</a>`)
})
for (const n of words) {
string = string.replace(RegExp(`\\b${n}\\b`, 'gi'), m => `<a href="#">${m}</a>`);
}
const newString = words.reduce((acc, n) => {
return acc.replace(RegExp(`\\b${n}\\b`, 'gi'), '<a href="#">$&</a>');
}, string);
words.forEach(word => string = string.toLowerCase().replace(word, `<a href="#">${word}</a>`));
console.log(string); // <a href="#">this</a> is <a href="#">cool</a>!
const words = ['cool', 'this'].map(word => new RegExp(`(${word})`, 'i'));
words.reduce((string, word) => string.replace(word, '<a href="#">$1</a>'), 'This is cool!');