0

Could you please tell me why my regex test fails when I use a lowercased a?

Here is my code:

  var a = "A0201418"

  if (/^A\d{7}$/.test(a)) {
      console.log('=====true');
  } else {
      console.log('false');
  }

Expected output "A0201418" . true
"a0201418" . true .

I want a pattern where start with "A" follow with 7 digit number

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
user944513
  • 12,247
  • 49
  • 168
  • 318

1 Answers1

2

Use the case-insensitive flag (i):

if (/^A\d{7}$/i.test(a)) {...}

If you're using the RegExp constructor, pass "i" as the second argument:

RegExp("^A\\d{7}$", "i")
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Joseph, please don't try to edit my code - comment it, and if I see it'll improve the quality of the post, I'll change it. – Jack Bashford Aug 29 '19 at 05:17