 <p>I've created a fiddle based on your code <a href="https://jsfiddle.net/d24z9ka4/" rel="nofollow">https://jsfiddle.net/d24z9ka4/</a> It's lousy but it works :-), and here is how:</p> <pre><code>text.onkeyup = function(evt) { // get how many times 'Brasil' occurs var counter = this.textContent.split(/(Brasil)/ig).length; // lastCount is the place of the last occurrence, or nothing if (counter &gt; 1 &amp;&amp; (!this.lastCount || this.lastCount &lt; counter) ) { this.lastCount = counter; // get's all places where 'Brasil' is written this.innerHTML = this.textContent.replace(/(Brasil)/ig, "&lt;b class='green'&gt;Brasil&lt;/b&gt;&amp;nbsp;"); // this moves cursor to last position available // based on this https://stackoverflow.com/questions/6249095/how-to-set-caretcursor-position-in-contenteditable-element-div var range = document.createRange(); var sel = window.getSelection(); // this is last node available, we'll go there var lastNode = text.childNodes.item(text.childNodes.length -1) range.setStart(lastNode, 1); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); } }; </code></pre>