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

254
Views
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 answers
Answer question

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