45

I need to have HTML's input elements pattern ignore the case of the value,
like have if the regex is /[a-z]*/ could I get it to match all uppercase letters too?
(I know I could just do /[a-zA-Z]*/, but that was an example.)

Nick Beeuwsaert
  • 1,518
  • 1
  • 11
  • 17

1 Answers1

68

I don't think it is possible.

  1. The specification on <input pattern> [1,2] specifies that

    • the pattern uses the ECMAScript (i.e. Javascript) flavor of regex

    • it is compiled "with the global, ignoreCase, and multiline flags disabled"

  2. In Javascript, the only way to make a regex ignore case is to set the modifier externally (/.../i). The PCRE syntax (?i) is not supported.

Therefore, the pattern is always case-sensitive and [a-zA-Z]* (i.e. making the regex itself explicitly case insensitive) is the only way to match the pattern in a case-insensitive way.

mikemaccana
  • 94,893
  • 84
  • 350
  • 433
kennytm
  • 491,404
  • 99
  • 1,053
  • 989
  • 3
    @dominicbri7 "with [...] flags ***disabled***". If the flag to ignore case is disabled, then it doesn't ignore case, so it's case sensitive. – Niet the Dark Absol Jun 10 '14 at 15:37
  • Great answer. (The pedant on my shoulder would have preferred you to have written 'verbosely' or 'explicitly' instead of 'manually' but it doesn't stop your answer being helpful) – Martin Joiner Dec 10 '17 at 15:00
  • 1
    so inconvenient when it comes to matching multiple terms in a case insensitive manner – oldboy Dec 26 '20 at 08:19