Level 2: Practical patterns · Lesson 16 of 30
Non-Capturing Groups
Use (?:...) to group tokens for repetition or alternation without creating a numbered capture. Your patterns stay tidy and your group numbers stay predictable.
Parentheses in regex do two jobs at once. They group several tokens so a quantifier or an alternation applies to the whole set, and they capture whatever that set matched so you can use it later. Very often you only want the first job. A non-capturing group, written (?:...), groups without capturing.
Consider a phone number split by dashes, such as 555-123-4567. You could write \d{3}-\d{3}-\d{4}, but if the number of blocks varies, it is cleaner to say 'one or more blocks of digits followed by a dash, then the final digits': (?:\d{3}-)+\d{4}. The + applies to the whole \d{3}- unit because the group binds it together, yet nothing is stored as group 1.
Alternation is the other common reason to group. The pattern \w+\.jpg|png does not mean 'a file ending in jpg or png'. The | has the lowest precedence, so it splits the entire pattern into \w+\.jpg on one side and png on the other. Wrapping the choice as \w+\.(?:jpg|png) limits the alternation to the extension, which is what you meant.
Why not just use ordinary parentheses everywhere? Captures cost a little memory and time, but the bigger issue is readability. When a pattern mixes groups you care about with groups that exist only for structure, the numbering becomes confusing. If you later add a structural group near the start, every $1, $2 and \1 after it shifts by one and your replacements silently break. Marking structural groups as non-capturing keeps the numbering limited to the parts you actually extract.
Non-capturing groups also make intent visible. A reader who sees (?: knows immediately that this group exists only to shape the pattern. A reader who sees a plain ( should be able to assume that the captured value is used somewhere, either in code or in a replacement string.
The syntax (?:...) is supported by every mainstream engine, including JavaScript, Python, PHP, Java, C#, Go, Rust and Ruby, so you can use it freely in portable patterns. It is one of the safest habits to build early.
A good rule of thumb: start every group as non-capturing, and only remove the ?: when you discover that you need the value. In the tester below, try changing (?:\d{3}-)+ to (\d{3}-)+ and notice that the full match does not change at all. Only the list of captured groups does.
Live tester
2 matches (JavaScript engine)
Call 555-123-4567 or 555-0000 today, not 12-34.
Examples
Numbers
/(?:\d{2}:){2}\d{2}/Start 09:15:30, end 17:45:00.
Matches: “09:15:30”, “17:45:00”
The group repeats the two-digit-plus-colon unit exactly twice, then the seconds follow.
Text
/\b(?:re|un)\w+/We redo and undo the review before lunch.
Matches: “redo”, “undo”, “review”
The alternation is limited to the prefix, so the rest of the word is matched by `\w+`.
Codes
/(?:[A-Z]{2}-)+\d+/Ticket AB-CD-42 and XY-7 are open.
Matches: “AB-CD-42”, “XY-7”
One or more letter-pair prefixes are grouped and repeated before the final number.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match each run of laughter made of repeated `ha`, such as `haha` or `hahaha`.
haha hahaha hey there
Task 2: Match file names that end in `.jpg` or `.png`, including the name part.
photo.jpg notes.txt logo.png draft.doc
Task 3: Match a dotted address made of four numbers, such as `10.0.0.1`.
Server 10.0.0.1 on port 8080, version 1.2