The problem is that I can not give spaces with the keyboard spacebar, I do not know if the same thing happens in other browsers, but in firefox it does not leave that space blank when the space bar is typed, someone has a solution
let textarea = document.querySelector('#textarea')
function x(el) {
el.focus();
if (typeof window.getSelection != "undefined" && typeof document.createRange != "undefined") {
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (typeof document.body.createTextRange != "undefined") {
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
}
function funcion()
{
textarea.innerHTML = textarea.textContent.replace(/@[a-zA-Z0-9_]+/gi, '<a href="https://www.dominio.com/">$&</a>')
x(textarea);
}
textarea.addEventListener('keyup', funcion, false)
<div id="textarea" contenteditable="true" style="height: 100px; padding: 10px; border: 1px solid #000"></div>