Regex Flags Referenz
Regular expression flags: global, ignoreCase, multiline, dotAll, unicode, sticky, und hasIndices.
Standard Flags
Flags modify the matching behavior of a regular expression and are specified after the closing delimiter. The global flag enables finding all matches rather than stopping at the first. The ignoreCase flag makes the pattern case-insensitive using simple case folding. The multiline flag changes caret and dollar to match line boundaries. Each flag is a single letter and flags can be combined, though some combinations have no additional effect.
- g
- Global, find all matches
- i
- Case-insensitive matching
- m
- Multiline, ^ and $ match line boundaries
- s
- DotAll, dot matches newline
- u
- Unicode mode, proper code point handling
/a/g /abc/i /^a/m /a.b/s /\p{L}/u Advanced Flags
The sticky flag y anchors the match at the lastIndex position without consuming characters before it, enabling efficient tokenization. The hasIndices flag d provides start and end indices for each capturing group in the match result. The unicode flag u enables property escapes and correct surrogate pair handling, and the v flag extends u with set operations in character classes. The v flag is the recommended replacement for u in modern engines.
Flag Interactions
The global and sticky flags both use lastIndex to track position but differ in anchoring behavior. With global, a failed match resets lastIndex to zero, while sticky leaves it unchanged. The dotAll and multiline flags are independent: dotAll affects the dot metacharacter and multiline affects the anchors. Combining unicode with ignoreCase uses full Unicode case folding, which matches more characters than simple folding and can surprise developers expecting ASCII-only behavior.