Course contentsShow
← JavaScript
Lesson 1673 of 2,72336. Regular ExpressionsFree lesson

The Case-Insensitive Flag (i)

Using the i flag to match letters regardless of case.

The Case-Insensitive Flag (i)

You'll learn how to match letters in regular expressions without worrying about uppercase or lowercase differences.

By default, regular expressions are case-sensitive — /cat/ will match "cat" but not "Cat" or "CAT". When you're searching for words regardless of how they're capitalized (user input, names, commands), this strict matching becomes a problem.

The case-insensitive flag i tells the regex engine to ignore case distinctions entirely. You add it after the closing slash of your pattern:

const pattern = /hello/i;

pattern.test("hello");  // true
pattern.test("Hello");  // true
pattern.test("HELLO");  // true
pattern.test("HeLLo");  // true

Think of it like a search function that treats "A" and "a" as identical twins — the pattern /cat/i will match "cat", "Cat", "CAT", "cAt", and any other combination.

Common Use Cases

Matching user input where capitalization varies:

const username = /admin/i;
username.test("Admin");   // true
username.test("ADMIN");   // true
username.test("admin");   // true

Finding words in text regardless of sentence position:

const word = /javascript/i;
const sentence = "JavaScript is great!";
word.test(sentence);  // true

You can combine the i flag with the g flag (global) you learned earlier:

const pattern = /cat/gi;
const text = "Cat, cat, CAT";
text.match(pattern);  // ["Cat", "cat", "CAT"]

Key Takeaway: The i flag makes your regex case-insensitive, treating uppercase and lowercase letters as equivalent during pattern matching.