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

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

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