Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Reference May 5, 2026

Regex case insensitive — in every language

Three ways to do it: a flag, an inline modifier, or restructuring with character classes.

The short answer

Add the case-insensitive flag. The exact spelling varies by language:

  • JavaScript: /pattern/i
  • Python: re.compile(pattern, re.IGNORECASE) or re.I
  • PCRE / Perl: /pattern/i
  • Java: Pattern.compile(pattern, Pattern.CASE_INSENSITIVE)
  • PHP: /pattern/i (PCRE-based)
  • Go: (?i)pattern
  • Ruby: /pattern/i

Inline modifiers

In most flavors, you can also enable case insensitivity inline at the start of the pattern:

(?i)pattern              entire pattern case-insensitive
(?i:abc)def              only "abc" case-insensitive, "def" sensitive
abc(?i)def(?-i)ghi       toggle modes mid-pattern (some flavors)

This is useful when constructing patterns programmatically — the flag is part of the pattern string itself, not a separate compile flag.

JavaScript

// Literal
const re = /hello/i;
re.test("Hello World")  // true
re.test("HELLO")        // true

// Constructor
const re2 = new RegExp("hello", "i");

// Inline
const re3 = /(?i)hello/  // NOTE: NOT supported in JS by default
                          // Use the i flag instead

JavaScript doesn't support inline (?i) the way PCRE does. Use the flag.

Python

import re

# Flag (preferred)
re.search(r"hello", text, re.IGNORECASE)
re.search(r"hello", text, re.I)        # short form

# Compile with flag
p = re.compile(r"hello", re.I)

# Inline
re.search(r"(?i)hello", text)          # same effect

Java

Pattern p = Pattern.compile("hello", Pattern.CASE_INSENSITIVE);
// Or with Unicode-aware case folding:
Pattern p = Pattern.compile("hello",
    Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

Go

// Inline only — no flag parameter
re := regexp.MustCompile(`(?i)hello`)
re.MatchString("HELLO")  // true

Without flags — using character classes

If you can't use flags (some embedded regex contexts, certain DSLs), you can write case-insensitive patterns explicitly:

Pattern: [Hh][Ee][Ll][Ll][Oo]
Matches: "Hello", "HELLO", "hELLO", etc.

Ugly but portable.

The Unicode case gotcha

Case insensitivity isn't straightforward in Unicode. The German "ß" historically uppercased to "SS" (two characters). Turkish has dotted and dotless i. By default, most engines do only ASCII case folding — "a" matches "A" but "ß" doesn't match "SS".

To enable Unicode-aware case folding:

  • JavaScript: add the u flag → /hello/iu
  • Python: default in Python 3 — works for most cases
  • Java: add UNICODE_CASE flag
  • PCRE: use /u with (*UTF) mode

Partial case insensitivity

Most flavors support a scoped inline modifier — make only part of a pattern case-insensitive:

(?i:abc)123      "abc" or "ABC", then literal "123"
abc(?i)def       "abc" sensitive, "def" insensitive (whole pattern from here)
abc(?i:def)ghi   sensitive, then insensitive "def", then sensitive again

JavaScript doesn't support this — you can't toggle case sensitivity within a single regex. Use multiple regexes or rewrite using character classes.

Performance

Case-insensitive matching is slightly slower because the engine has to consider two characters at each position. For most patterns this is negligible. For performance-critical code with very large inputs, consider:

  • Pre-lowercasing the input once, then using a lowercase-only regex
  • Using explicit character classes instead of the flag

The takeaway

For 99% of cases, use the language's case-insensitive flag. It's clear, idiomatic, and well-optimized.

For partial case insensitivity or when you can't pass a flag, use inline modifiers (?i:...) in flavors that support them, or fall back to explicit character classes.


Related reading


Try this pattern in the explainer

Paste any regex into the live explainer and see what each token means, with example matches in real time.

Open the regex explainer →