• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

111
Vistas
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?

almost 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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 ");
}
almost 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda