GUIDE深度 · Deep dive

Regex Capture Groups

Parentheses change precedence and capture the matched text. Reference captures with $1/$2 in the replacement; use (?:...) for grouping without capture, and (?<name>...) for named groups.

更新 2026-10-31 · 公式可核验

Capture and reference

(\w+)@(\w+\.\w+) has two capture groups.

The replacement $1 [at] $2 keeps the user and domain.

Non-capturing and named

(?:...) groups without capturing, so numbering stays stable.

(?<user>...) is referenced as $<user>, which is more readable.

FAQ

What is $0?

JavaScript uses $& for the whole match, not $0.

What if a group did not match?

It becomes an empty string or undefined; test optional groups first.

Sources

Related calculators

Regex Replace ToolEnter a regex, replacement and text to replace matches and show the count, with $1 group support.

More guides

Common Regex Replace UsesRead guide