replace the word "dog" with the word "cat" in the following sentence: "the quick brown fox jumps over the lazy dog." in javascript

index.tsx
const sentence = "the quick brown fox jumps over the lazy dog.";
const newSentence = sentence.replace(/dog/g, "cat");

console.log(newSentence);
// Output: "the quick brown fox jumps over the lazy cat."
203 chars
6 lines

Explanation: We use the replace() method on the string sentence to search for all occurrences of the word "dog" using a regular expression with the g (global) flag. The second argument is the word "cat", which replaces every occurrence of "dog". Finally, we log the new sentence with the replaced word to the console.

gistlibby LogSnag