Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

368
Vistas
How to search exact or closest word in array, using javaScript RegEx?

In my app I have implemented local search using Regular expressions, I have problem, I could not find solution even after much searching, as you can see I have words array, Which I filter based on condition, I want to implement something like that:

if user writes ele into the search box, I want to get ['electric', 'elektric'], same should happen if user writes ric into the input field result: ['electric', 'elektric'], but In case the user enters the exact name, for example electric result should be : ['electric'] At last, if input will be cooling result should be ['engine cooling']

As you can see, My goal is to system search exact word or exact letters or the closest logical option. I will take any advice, thanx

let words = ['electric', 'elektric', 'engine cooling']

function bestMatch(event){
 let match = words.filter((e) => new  RegExp(`^${event.value}`,"ig").test(e));
 console.log(match)
}
<input type="text" placeholder="best match" onchange="bestMatch(this)" />

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Add .* before and after to get the regex to work so that position in word doesn't matter, though regex is unneccesary based on your requirements. Instead you can use includes. Finally onkeyup might be better/faster in getting results

With regex:

let words = ['electric', 'elektric', 'engine cooling']

function bestMatch(event){
 let match = words.filter((e) => new  RegExp(`^.*${event.value}.*`,"ig").test(e));
 console.log(match)
}
<input type="text" placeholder="best match" onchange="bestMatch(this)" />
Without regex (and using onkeyup):
let words = ['electric', 'elektric', 'engine cooling']

function bestMatch(event){
 let match = words.filter((e) => e.includes(event.value.toLowerCase()))
 console.log(match)
}
<input type="text" placeholder="best match" onkeyup="bestMatch(this)" />

about 4 years ago · Juan Pablo Isaza 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda