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

520
Visualizações
How to use match() method when I have a variable dynamically with case insensitive?

Suppose I have case insensitive strings to be compared like,

const a = 'b'

const c = 'B'

So for comparing these, I used the match() method, but it works for static strings like we can use / for doing case insensitive comparison.

Eg. a.match(/b/), but I need to do it dynamically like if I have a variable, test1 = 'b' and test2 = 'B', but when doing like this test1.match(/test2/) it gets "test2" as string but I need the value('B') in it.

As I have tried like the following image

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

0

You can use the RegExp constructor to make a dynamic regular expression that depends on the variable's value.

function isMatch(str, testCase) {
  var rgx = new RegExp(testCase);
  return str.match(rgx);
}

console.log(isMatch("b", "b"));
console.log(isMatch("b", "B"));
console.log(isMatch("hello world", "hello"));
console.log(isMatch("hello world", "hi"));

Check MDN for more info.

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