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

256
Visualizações
Javascript regex pattern to match for Function keys from F1-F12 using pattern test function

Javascript regex pattern to match for Function keys from F1-F12 using pattern test function

I tried below but gives false as output, excepted is true

let text = "F11";
let pattern = /^[F][1-12]$/;
let result = pattern.test(text); console.log(result);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This regular expression matches F1-F12: /^F[1-9](?:(?<=1)(?:0|1|2))?$/:

let re = /^F[1-9](?:(?<=1)(?:0|1|2))?$/;

for (var i = 0; i < 16; i++){
  let text = `F${i}`;
  console.log(`${text}: ${re.test(text)}`);
}

To break it down:

  • ^ matches the start of input; it will only match if it is at the beginning
  • F matches a literal 'F'
  • [1-9] matches a character between 1 and 9
  • (?:(?<=1)(?:0|1|2)) is where it matches 11 and 12
    • (?<=1)(?:0|1|2)) is a lookbehind assertion: it will only match the second part if the first is true
      • (?:0|1|2) matches the 0, 1, or 2 for the 10-12
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