JavaScript, Replace All Occurrences Of String


How to replace all occurrences of string using JavaScript?

Let’s say you have the sentence, “Nory was a Catholic because her mother was a Catholic”

To replace all occurrences of Catholic word from the above sentence, you can make use of the String.replaceAll method.

let word = "Catholic";
let sentence = "Nory was a Catholic because her mother was a Catholic"
let new_sentence = sentence.replaceAll('Catholic','Jewish');
console.log(new_sentence)

// "Nory was a Jewish because her mother was a Jewish"