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

159
Views
Searching through array of strings to match a keyword string that contains wildcard letters in Javascript?

I have a keyword string that contains at least 1 wildcard letter; "?". Each "?" represents a single lowercase letter between a-z. I need to search through an array of strings to find which letters match the keyword.

let input = ["happy", "have", "harvest", "harmonica"]

let keyword = "ha???"

result = "happy"

I've tried to replace the wildcard letter ? with regex, but it does not seem to work because it would return "ha/([a-z])+/g" and would not compare like I wanted it to.

I also tried to just replace ? with "", but obviously it would match all of the items so it doesn't work.

Is there any suggestions?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could just replace ? with [a-z], and add start and end of string anchors (^ and $) to your regex. For example:

const input = ["happy", "have", "harvest", "harmonica"]

const keyword = "ha???"

const regex = new RegExp('^' + keyword.replace(/\?/g, '[a-z]') + '$')

const result = input.filter(w => regex.test(w))

console.log(result)

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!