Case-Insensitive Matching with 'i'
You'll learn how to use the i flag to make regex patterns ignore uppercase and lowercase differences.
By default, regular expressions are case-sensitive—the pattern /hello/ matches "hello" but not "Hello" or "HELLO". This is often too strict when you want to find text regardless of how it's capitalized.
The i flag (short for "ignore case") tells the regex engine to treat uppercase and lowercase letters as equivalent. You add it right after the closing slash of a regex literal: /pattern/i.
How It Works
Think of the i flag like putting on special glasses that make all letters look the same height—you can no longer distinguish between capital and lowercase forms.
// Without 'i' flag - case-sensitive
/hello/.test("hello"); // true
/hello/.test("Hello"); // false
/hello/.test("HELLO"); // false
// With 'i' flag - case-insensitive
/hello/i.test("hello"); // true
/hello/i.test("Hello"); // true
/hello/i.test("HELLO"); // true
/hello/i.test("HeLLo"); // true
Combining with Other Flags
You can use i alongside other flags like g (global):
const text = "Cat cat CAT";
text.match(/cat/gi); // ["Cat", "cat", "CAT"]
Here, g finds all matches while i ignores case.
When to Use It
The i flag is perfect for user input validation, searching text, or anywhere capitalization shouldn't matter:
/^yes$/i.test("YES"); // true
/^yes$/i.test("yes"); // true
/^yes$/i.test("Yes"); // true
Key Takeaway: The i flag makes regex patterns case-insensitive, treating A and a (and all other letters) as equivalent—essential for flexible text matching.