@russian-dima I used your example and was able to make it work.
Use /gi at the end where g stands for global and it’ll replace all occurrences, i stands for case-insenstive.
This is the code I used.
let str = $w("#text65").text;
let res = str.replace(/(^|\W)carbon($|\W)/gi, `<mark>${newWord}</mark>`);
console.log(res);
$w("#text65").html = res;
However, now when I use this code, my text loses all its formatting. For example, the color of the text is now different compared to the other text on the same page. And there are no paragraphs present either.