Regex · <code>(?:...)</code>
Non-capturing groups
(?:...) groups a subpattern for quantifiers and alternation without creating a capture.
Key facts
| Syntax | <code>(?:...)</code> |
|---|---|
| Captures | None |
| Numbering | Does not consume a group number |
| Use | Apply a quantifier or alternation to several tokens |
| Atomic group | <code>(?>...)</code> in PCRE and Java |
| JavaScript | Supports (?:...), not atomic groups |
| Overhead | Slightly lower than a capturing group |
Grouping without side effects
(?:cat|dog)s? matches “cat”, “cats”, “dog” or “dogs” while capturing nothing. The parentheses are needed to bind the alternation, but the captured text is not wanted, so the non-capturing form keeps the match array and the group numbering clean.
In long patterns this is not just cosmetic: inserting a capturing group shifts every later number, breaking replacements and backreferences. Defaulting to (?:...) unless a capture is needed makes edits safer.
Atomic groups are a different tool
An atomic group (?>...) also does not capture, but its purpose is to forbid backtracking into the group after it has matched. It is a performance and semantics control, not a naming convenience, and it is unavailable in JavaScript.
POSIX-style engines and some older tools have their own group variants. When a pattern must run in several languages, stick to capturing and non-capturing groups and express the rest with explicit classes.
Frequently asked questions
What does (?: ) do in a regex?
It groups without capturing, so the subpattern can be quantified or alternated without adding a numbered group.
Is a non-capturing group faster?
Slightly, because no capture is stored, but the difference matters far less than avoiding unnecessary backtracking.
Does JavaScript support atomic groups?
No. It supports non-capturing groups but not the (?>...) atomic syntax.