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

177
Vistas
Javascript regular expression for search from starting letter?

I want add search functionality. I need to search any document by starting letter.

Suppose, we have to some document like-

Apple Industry
Samsung Industry
Xioami Industry
Ahua Industry
Sumatech Industry

When user search a then it should give the result which one start with a like-

Ahua Industry
Apple Industry

You can see the result show alphabetically

I already create a regex like this-

{ Industry: { '$regex': 'a', '$options': 'i' } }

But it show all the content that contain a.

Like-

It show every content because all the content contain a.

How can I solve that. Can anyone help me.

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

0

You need to add the startWith modifier to your regex. I'm not sure how this object gets converted to the regex, but here you go:

{ Industry: { '$regex': '^a.*', '$options': 'i' } }

function matchText(query) {

  const text = ['Apple Industry',
  'Samsung Industry',
  'Xioami Industry',
  'Ahua Industry',
  'Sumatech Industry'
  ];

 const regex = new RegExp(`^${query}.*`, 'i');

 text.forEach((item) => console.log(item.match(regex))); 
}

matchText('a');
matchText('ap');

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