I want a contenteditable div to call a function which constantly reviews its contents and replaces patterns within it (think Markdown). My attempts resulted in the cursor constantly positioning itself to the start of the div with the text being inserted in backwards order. Additionally, even if a (regex style replacement) pattern matches, replacements with HTML tags (such as bold) ends up not terminating, leaving the rest of the div bold. A simple code sample duplicating these issues follows:
<body>
    <div id = "content" contenteditable = "true" oninput = "reformat()"></div>
</body>
// JS //
var content = document.getElementById("content");
function reformat()
{
    content.innerHTML = content.innerHTML.replace(/dogs/g, "<b>cat</b>");
}
Basically, I want it to replace "dogs" with "cat" in bold and nothing else abnormal as the user is typing. My last resort is to convert non-contenteditable divs to act as contenteditable ones to avoid issues, but it involves reinventing the wheel for something that should be seemingly trivial. How should I approach this?
I have visited other posts such as replace string in contenteditable div but the cursor is still stuck and I would like my answer in vanilla JS if possible. If there's a platform other than HTML/JS/CSS (Visual Studio, Python tkinter, etc) that this is more suited towards, please let me know.