Dotall, sticky and Unicode flags
The s flag lets . match newlines, y pins matches to lastIndex, u and v switch to code points, d reports positions. Includes Python's re.S and re.A.
Flags: ignore case, global and multilineThe dot and newlines
Dotall, sticky and Unicode flags
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- s (dotall) lets . match a newline. Without it, /start.end/ fails on start, newline, end. The portable alternative is [\s\S]. Python's flag is re.S and the inline form is (?s).
- y (sticky) means the match must begin exactly at lastIndex, not anywhere after it. That makes a tokenizer loop possible, because each rule can only claim the text right here.
- u switches matching from UTF-16 code units to code points, so /^.$/u matches one emoji and /^.$/ does not. In u mode, a useless escape such as \- is a SyntaxError.
- v is a stricter superset of u that adds set operations in classes, like [\p{L}--[a-z]]. You cannot write both u and v on one regex.
- d produces match.indices with the start and end of every group. Python treats \w as Unicode by default for text patterns and re.A restricts it to ASCII, the reverse of JavaScript.
Flags that change what the engine counts as text
The last lesson covered three flags that change when a match happens. These four change what the engine sees: where lines end, where it may start and what a character is. Each has a specific problem it exists to solve.
s: let the dot cross lines
The dot matches any character except a newline. That default is right for line-oriented text and wrong the moment a thing spans lines, like a block comment.
const t = 'start\nend';
/start.end/.test(t) // false
/start.end/s.test(t) // true
/start[\s\S]end/.test(t) // true
/<!--.*?-->/.exec('<!-- a\nb -->') // null
/<!--.*?-->/s.exec('<!-- a\nb -->')[0] // '<!-- a\nb -->'
The s flag, called dotall, lets . match a newline too. The older spelling is [\s\S] (“any whitespace or any non-whitespace”, so any character), and that works with or without the flag. In Python the same switch is re.S: re.search('start.end', t, re.S) matched, and so did the inline form (?s)start.end. Perl needs it as well: "a\nb" =~ /a.b/ was no match and /a.b/s matched. pcre2grep -M (multi-line mode) found a.b across a line break only with (?s) added.
y: sticky, match exactly here
A normal g regex starts at lastIndex and scans forward until it finds a match. A sticky regex is stricter: the match has to start at lastIndex, or it fails.
const re = /foo/y;
re.test('xfoo'), re.lastIndex // false 0
re.lastIndex = 1;
re.test('xfoo'), re.lastIndex // true 4
/foo/g.test('xfoo') // true (g scans ahead, y does not)
That is exactly what a tokenizer wants. At each position, you ask each rule “do you match right here?” and the first one that says yes claims the text. I wrote a small one with four sticky rules, number, identifier, operator and whitespace:
tokenize('x = 42 + y')
// [ 'id:x', 'op:=', 'num:42', 'op:+', 'id:y' ]
tokenize('x = $') // throws: bad char "$" at 4
With g, a rule would skip over a bad character, find a later match and silently drop the text in between. With y, a character no rule accepts stops the loop immediately, and the error points at the exact index. Python has no y flag. Its pattern.match(text, pos) plays the same role: on 'xxfoo' with pos 1, match returned None while search found foo at index 2.
u and v: characters, not code units
JavaScript strings are sequences of 16-bit units. An emoji like 😀 takes two of them. Without u, the regex engine sees two units and treats each as a “character”:
const e = '😀';
e.length // 2
[...e].length // 1
/^.$/.test(e) // false
/^.$/u.test(e) // true
'😀'.match(/./g).length // 2
'😀'.match(/./gu).length // 1
/\u{1F600}/u.test(e) // true
/\u{1F600}/.test(e) // false
u turns on code-point matching and also unlocks the \u{...} escape. It makes the syntax stricter, too: new RegExp('\\-', 'u') threw SyntaxError: ... Invalid escape, while without u that useless escape was accepted. The next group of lessons on Unicode goes deep here, including \p{...} property escapes.
The v flag is the newer, stricter version of u. In Node 26 I could subtract one class from another: 'abcXYZ'.match(/[\p{L}--[a-z]]/gv) returned ['X','Y','Z'], letters minus lowercase ASCII. And new RegExp('a', 'uv') threw SyntaxError: Invalid flags, so it is one or the other. /a/v.unicodeSets is true and /a/u.unicodeSets is false.
Python goes the opposite way on defaults. A text pattern is Unicode-aware from the start (re.U is on, as the default for str), so re.search(r'\w', 'é') matched. re.A restricts \w, \d and \s to ASCII, and the same search with re.A did not match. JavaScript’s \w is ASCII-only: /\w/.test('é') was false, with or without u. If you move a pattern between the two, that difference changes results.
d: where exactly did it match?
Add d and the match gets an indices array giving each group’s start and end:
const m = /(?<y>\d{4})-(?<mo>\d{2})/d.exec('on 2026-11-23');
m.indices[0] // [ 3, 10 ]
m.indices[1] // [ 3, 7 ]
m.indices.groups.mo // [ 8, 10 ]
You’d use it for highlighting or for editing the original string in place. Together with the other flags, a regex can carry dgimsuy (/a/dgimsuy.flags printed dgimsuy), and v replaces u rather than adding to it.
About the video
The embedded clip is a section of LearnAwesome’s “Learn Regex Flags – Global, Case-Insensitive, Multiline & More Explained | Regex Tutorial”, published 3 August 2024 and 21 minutes 18 seconds long. The clip runs from 6:05 to 20:51. It covers the chapters on the indices flag, ignore case, dot-all, multiline, the Unicode flags and the sticky flag, each with a demo. Ignore case and multiline are in the clip only because the chapters sit between the ones I want, so they repeat the previous lesson, which uses the earlier part of the same video. The chapter list names regex101 and JavaScript demos for every flag. I couldn’t read a transcript, so I can’t say how it treats u versus v or whether it builds anything like the tokenizer. English per its title and description.
What I did not run
regex101’s flag panel, the v flag in browsers other than Node, PHP and GNU grep -z.
Next
Flags set from outside the pattern are fine until a pattern needs one flag for half of it. Inline flags and verbose mode shows how to switch flags inside the pattern and how to write long patterns with comments.
Check yourself
Why does /<!--.*?-->/ fail on a comment that spans two lines, and what are two fixes?
The dot does not match a newline, so the lazy .*? cannot cross the line break. Add the s flag, /<!--.*?-->/s, or replace . with [\s\S].
With lastIndex set to 1 on the string 'xxfoo', what do /foo/g and /foo/y return from test?
The g version returns true, because it scans forward from index 1 and finds foo at index 2. The y version returns false, because foo does not start exactly at index 1. Sticky means here or nowhere.
How many matches does '😀'.match(/./g) find, and with the u flag?
Without u it finds two, because the emoji is two UTF-16 code units and each is matched alone. With u it finds one, since . matches a whole code point.
Does JavaScript's /\w/ match the letter é, and does Python's?
JavaScript's does not, even with the u flag. Python's does for a normal text pattern, because \w is Unicode-aware by default. Passing re.A in Python makes it ASCII-only.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.