Skip to main content
Regexflux
Lesson 11 of 120 completed

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/gi

Matches 'error' in any case with the 'i' flag

ERROR: timeout
An error occurred
WARNING: disk full
error code 500
/^\d/gm

With 'm' flag, ^ matches the start of each line

1 first line 2 second line not a number
abc 123 456

Exercise

Write a case-insensitive pattern that matches the word "warning" anywhere in a string. Make sure to enable the `i` flag.

Your pattern:

Must match

WARNING: disk full
This is a Warning
warning detected in log
CRITICAL WARNING issued

Must not match

error occurred
info message
debug trace
fatal exception