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

164
Visualizações
javascript word in sentence matching

I'm currently trying to find matching words within sentences, and I have a method that mostly works. It's working on a HTMLNode.textContent where I currently use the indexOf method to find a match.

let exists = node.textContent.indexOf(x.searchText) > -1

I need it to match exactly, including any spaces and special characters in the word, which works with indexOf, but I also want to be able to create exceptions to this exact search.

For example the word "Burnett’s" should be equal to "Burnett's" despite the apostrophe formats being different.

There are quite a lot of checks when this runs, so preferably I want something fast without having to check every word if it contains either of those apostrophes and if so remove it so it's not part of the comparison. Are there any better ways to check for or do it on?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You could replace every exception variant to a single one to check against:

function cleanText(str) {
    return str.replace(/[`’]/g,"'");
}
            
let exists = cleanText(node.textContent).indexOf(cleanText(x.searchText)) > -1;

You can easily chain additional exceptions in the cleanText function, for example:

function cleanText(str) {
    return str.replace(/[`’]/g,"'").replace(/@/g, " at ").replace(/&/g, " and ");
}
about 4 years ago · Santiago Trujillo Relatório
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