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

171
Visualizações
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 Respostas
Responde à pergunta

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 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