Skip to main content
Regexflux
Lesson 7 of 120 completed

Non-Capturing Groups

Group patterns for structure without creating numbered capture references.· 6 min

Concept

Non-capturing groups (?:...) group elements for quantifiers or alternation without adding a numbered capture. This keeps your capture group numbering clean.

When to use (?:...) instead of (...): - You need grouping for alternation or a quantifier, but don't need to reference the captured text - You want to avoid shifting capture group numbers when adding inner groups - You're building patterns that other code will use — fewer captures means less confusion

Example: (?:https?|ftp):// groups the protocol options without capturing them, leaving group $1 available for something more meaningful.

/(?:https?|ftp)://[\w.-]+/g

Groups the protocol as a non-capturing alternative — the URL host isn't shifted to group 2

https://example.com
http://test.org
ftp://files.net
ssh://server.io
/(?:very ){2,3}large/g

Matches 'very very large' or 'very very very large' without capturing 'very '

very very large
very very very large
very large
very very very very large

Exercise

Write a pattern matching "colour" or "color" using a non-capturing group for the optional "u".

Your pattern:

Must match

favourite colour
red color scheme
blue colour
Color palette

Must not match

colouur
colr
coolour
paint