The replaceAll() of a String returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.

    
    const sentence = "Ruth's dog is cuter than your dog!";

    const sentenceReplaceAll = sentence.replaceAll('dog', 'monkey');

    console.log(sentenceReplaceAll);
    // "Ruth's monkey is cuter than your monkey!"


    // Another example: regex - global flag required
    const regex = /Dog/gi;

    const sentenceReplaceAllRegEx = sentence.replaceAll(regex, 'ferret');

    console.log(sentenceReplaceAllRegEx);
    // "Ruth's ferret is cuter than your ferret!"