Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!