Regex Flags
Flags modify global matching behavior — case sensitivity, multiline anchors, and more.· 6 min
Concept
Flags are appended after the closing delimiter of a regex to change how it works:
- g — **global**: find all matches (without it, only the first match is returned)
- i — **case-insensitive**: [a-z] also matches uppercase letters
- m — **multiline**: ^ matches start of each line, $ matches end of each line
- s — **dotall**: . also matches newline characters \n
- u — **unicode**: enables full Unicode support and stricter parsing
Flags can be combined: gi = global + case-insensitive.
In the tester, toggle flags with the buttons above the pattern input. In code, you pass them as a string: new RegExp('pattern', 'gi') or as a literal: /pattern/gi.
/error/giMatches 'error' in any case with the 'i' flag
/^\d/gmWith 'm' flag, ^ matches the start of each line
Exercise
Write a case-insensitive pattern that matches the word "warning" anywhere in a string. Make sure to enable the `i` flag.
Your pattern: