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

Case Sensitivity by Default

Regex is case-sensitive unless you use the i flag; common source of missed matches.

Case Sensitivity by Default

You'll learn why your regex patterns might be missing matches and how case sensitivity is the likely culprit.

By default, regular expressions in JavaScript treat uppercase and lowercase letters as completely different characters. This means a pattern like /hello/ will match "hello" but not "Hello", "HELLO", or any other capitalization variant. Many beginners expect regex to be flexible about case, but it's actually quite strict unless you explicitly tell it otherwise.

This is one of the most common sources of frustration when patterns mysteriously fail to match text that "looks right" to human eyes.

How Case Sensitivity Works

When you write a regex without any special instructions, every letter is matched exactly:

const pattern = /cat/;

console.log(pattern.test("cat"));   // true
console.log(pattern.test("Cat"));   // false
console.log(pattern.test("CAT"));   // false

The pattern /cat/ only matches the exact sequence "c", "a", "t" in lowercase.

The Solution: The i Flag

You've already learned about the case-insensitive flag (i). This tells the regex engine to ignore case differences:

const caseInsensitive = /cat/i;

console.log(caseInsensitive.test("cat"));   // true
console.log(caseInsensitive.test("Cat"));   // true
console.log(caseInsensitive.test("CAT"));   // true
console.log(caseInsensitive.test("CaT"));   // true

Now all capitalizations match successfully.

When This Bites You

User input is unpredictable. Someone searching for "javascript" might type "JavaScript" or "JAVASCRIPT". Without the i flag, your pattern will miss valid matches and your application will appear broken.

Key Takeaway: Regular expressions are case-sensitive by default—always consider whether you need the i flag to handle real-world text variations.