Regex: Groups, Flags & Lookaround in JavaScript
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.
Capture and name
(\d+)-(\d+) captures anonymously; (?<area>\d+) names the group — matches.groups.area reads itself.
Flags reshape
i ignores case, m anchors per line, s dots newlines, u fixes astral handling, g collects all.
Lookaround asserts
(?=.*\d) requires a digit ahead without consuming — stacked conditions for validation rules.
Pick the method
.test for booleans, .match/.matchAll for details, .exec in loops — question-shaped method choice.
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"));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.