What Are Regular Expression Flags?
Regular expression flags are special modifiers that change how patterns match text. They provide extra instructions to the regex engine about case sensitivity, search scope, and text boundaries. These flags are typically added after the closing delimiter in regex syntax.
Common Regular Expression Flags
Different programming languages share similar flag conventions. Here are the most common ones:
- **i (case-insensitive)**: Matches lowercase and uppercase letters equally
- **g (global)**: Finds all matches in text instead of stopping at the first match
- **m (multiline)**: Treats ^ and $ as matching line starts/ends, not just text boundaries
- **s (dotall)**: Allows . to match newline characters
- **u (unicode)**: Enables full Unicode matching support
- **y (sticky)**: Continues matching from the last position only
How Flags Modify Pattern Behavior
Let's see flags in action with examples:
1. Without flags: `/hello/` matches "hello" but not "HELLO"
2. With 'i' flag: `/hello/i` matches "hello\