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

287
Visualizações
Filtering with regex in javascript

I am working currently on a project where I want to filter against an input I give. At the moment, my approach is:

var num = some.innerText;
var filter = input.value.toUpperCase().trim();

if (num.toUpperCase() == filter) {do something}

If my filter is 11 I will find only the num where the text is exactly 11, but sometimes my numbers have letters like 11A or 11B and I would like to find all instances of 11 when I filter for that. My idea is to use an or that I either check for an exact match or a match, where after the filter follows any letter character. I wanted to use regex, but this doesn't seem to work.

My regex approach

if (num.toUpperCase() == filter || num.toUpperCase() == filter + /[a-zA-Z]/) 
{do something} 

If I do this it works, but only for the letter d and I dont want to write 24 or conditions

if (num.toUpperCase() == filter || num.toUpperCase() == filter + "D") {do something}

I tried

if (num.toUpperCase().match(filter)  {do something} 

, but I dont want to find 11, 123, and so on when I enter 1 into the filter.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This seems to work perfectly for my purpose. I can now get the results 11D, 11E, 11B and so on when entering 11 in to the filter and also 11D if I use that as the filter.

if (num.replace(/\D+/,'') == filter || num == filter) {do something}

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