Level 2: Practical patterns · Lesson 15 of 30
Flags (g i m s u y)
Flags change how a whole pattern behaves: global search, case-insensitive matching, multiline anchors, dot-matches-newline, Unicode mode and sticky matching.
Flags, also called modifiers or options, are settings that apply to an entire pattern. In JavaScript they come after the closing slash, as in /hello/gi, or as the second argument of new RegExp("hello", "gi"). Other languages pass them as options, but the ideas are the same.
g is global. Without it, JavaScript methods like match return only the first match; with it, they find every match. Python, PHP, Java, Go, C#, Rust and Ruby have no g flag; instead you choose a function, such as Python's re.findall, PHP's preg_match_all, or Go's FindAllString.
i makes matching case-insensitive, so apple matches Apple and APPLE. Python spells it re.IGNORECASE or re.I, Java uses Pattern.CASE_INSENSITIVE, C# uses RegexOptions.IgnoreCase, and PHP, Ruby, Go and Rust accept i, often as the inline form (?i) at the start of the pattern.
m is multiline. It makes ^ and $ match at the start and end of every line, not just the whole text. Python uses re.MULTILINE, Java Pattern.MULTILINE, and C# RegexOptions.Multiline. Remember that Ruby is different: its anchors are always line-based, and its m flag means something else.
s is dotall, sometimes called single-line mode. It lets . match newline characters, so start.*end can span several lines. Python uses re.DOTALL, Java Pattern.DOTALL, C# RegexOptions.Singleline, and Go and Rust the inline (?s). In Ruby, this behaviour is the m flag, which is a frequent source of confusion.
u turns on Unicode mode in JavaScript. It makes the engine treat characters outside the basic plane, such as many emoji, as single characters, enables \u{...} escapes and Unicode property classes like \p{L}, and makes the parser stricter about invalid escapes. Newer JavaScript also has a v flag that extends this further. Python 3, Go and Rust patterns are Unicode-aware by default.
y is sticky, a JavaScript-specific flag. A sticky regex only matches at exactly the position stored in lastIndex, without searching ahead. It is mainly used when writing tokenizers that read text piece by piece. The practice checker on this site always searches globally and ignores g and y in your answer, so focus on i, m and s for the tasks.
Live tester
3 matches (JavaScript engine)
Hello hello HELLO help
Examples
Numbers
/^\d+/gm10 apples 20 pears 30 plums
Matches: “10”, “20”, “30”
The m flag lets the start anchor match at each line, so all three numbers are found.
Text
/cat/giCat CAT cat
Matches: “Cat”, “CAT”, “cat”
The i flag ignores case, so all three spellings match.
Codes
/a.b/gsa b axb
Matches: “a b”, “axb”
The s flag lets the dot match the newline, so a, newline, b is a match too.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match the word apple in any capitalisation, but not apply.
Apple apple APPLE apply
Task 2: Match heading lines that start with a hash sign, but not a hash in the middle of a line.
# Title text # not # Next
Task 3: Match the whole note block from [note] to [/note], across line breaks.
[note] line 1 line 2 [/note] plain line