Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
How to replace matched expressions without interrupting the user input on contenteditable?

I am looking for specific regex matches in a text in a contenteditable div. I do find the correct matches:

<script>
const regex = /my_regex/g;
let content = $("#contenteditable").text();
let matches = content.match(regex);            

if (matches) {
    matches.forEach((match, groupIndex) => {
    // it finds the correct matches.    
    });
}
</script>

I want to highlight the matches in the text by wrapping each match in a <span> element with some styling

At first I tried using content.replace(//..) and then document.getElementById('contenteditable').textContent = content; or other methods using innerHTML but realized that it just reloads everything and not allowing the user to keep typing in the contenteditable div.

So I think the only way to do it is actually get the position of each match and replace each occurrence alone

So I found this post: Get a range's start and end offset's relative to its parent container

But then even if I have the caret position for each match, how can I replace only that? Is there any less complicated way to replace matches "on the fly" (as the user keeps typing without interrupting him), or it has to be done in these complicated ways?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda