Learn JavaScript
❯ strings_regex.regex_advanced

Regex: Groups, Flags & Lookaround in JavaScript

$concept

Regex power concentrates in groups and flags: (...) captures (match[1], match[2]), (?:...) groups without capturing, named groups (?<year>\d{4}) self-document matches, and flags reshape matching — i (case-insensitive), m (^/$ per line), s (. matches newlines), u (unicode), v (extended sets).

Lookaround asserts without consuming: (?=...) lookahead, (?!...) negative lookahead (password rules: (?=.*\d)), (?<=...)/(?<!...) lookbehind. Split logic (test/match/matchAll/exec) picks single-vs-all and detail level; invalid patterns throw SyntaxError at construction.

$how it works
1

Capture and name

(\d+)-(\d+) captures anonymously; (?<area>\d+) names the group — matches.groups.area reads itself.

2

Flags reshape

i ignores case, m anchors per line, s dots newlines, u fixes astral handling, g collects all.

3

Lookaround asserts

(?=.*\d) requires a digit ahead without consuming — stacked conditions for validation rules.

4

Pick the method

.test for booleans, .match/.matchAll for details, .exec in loops — question-shaped method choice.

$example
let m = "2026-09-20".match(/(?<y>\d{4})-(?<mo>\d{2})-(?<d>\d{2})/);
console.log(m.groups.mo);
console.log(/cat/i.test("CAT"));
Output
09
true
1Named groups capture year, month, day — groups.mo reads 09 by name.
2The i flag makes CAT match cat — true.
3Structured extraction plus flag-driven matching in four lines.
$use cases
Structured extraction (dates, versions, codes) with named groups
Validation rules composed from lookahead conditions
Case-insensitive and multiline matching via flags
matchAll iteration over every occurrence with details

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.