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.
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.