Substitutions are allowed only within replacement patterns. For similar functionality within regular expressions, use a backreference.
For example, the replacement pattern (d)*$1m inserts the string "(d)*" followed by the substring matched by
the first capturing group, if any, followed by the string "m".
The * character is not recognized as a metacharacter within a replacement pattern.
Similarly, $ patterns are not recognized within regular expression matching patterns.
Within regular expressions, $ designates the end of the string.
Substitutions and their behavior in the context of replace pattern:
$n
Where n is a positive integer.
Substitutes the last substring matched by group number n.
$$
Substitutes a single "$" character.
$&
Substitutes a copy of the entire match itself.
\l
\u
\L
\U
\E
\n
\r
carriage return character
\t
Examples
/( ){3}/\t/g
Replace any occurence of three spaces with tab.
/\b(\w+)\s+\1\b/$1/g
Remove any duplicated word.
/\b(\w+)\b/($1)/g
Enclose any word with brackets.
/^\s*(.*?)\s*$/$1/s
Remove leading and trailing spaces from a string.
/\b(\w+)\b/\u\L$1/g