Script Valley
Regex: Actually Useful Patterns
Groups and CapturingLesson 2.3

Non-capturing groups and grouping without capturing

non-capturing group syntax, performance difference, grouping for quantifiers, alternation scope, when to use

Group Without the Overhead of Capturing

Non-capturing groups

Every capturing group allocates a slot in the match result. If you only need grouping for quantifier or alternation scope — not extraction — use a non-capturing group: (?:pattern).

// Group for quantifier, no capture needed
/(?:ab)+/.test('ababab') // true — matches 'ab' repeated

// Alternation scope
/^(?:cat|dog)s?$/.test('cats') // true

Non-capturing groups are also faster in engines that optimize capture tracking.

Up next

Backreferences in regex to match repeated text

Sign in to track progress