The M-x replace-string command replaces exact matches for a single string. The similar command M-x replace-regexp replaces any match for a specified pattern.
replace-regexp, the newstring need not be constant: it
can refer to all or part of what is matched by the regexp.
`\&' in newstring stands for the entire match being replaced.
`\d' in newstring, where d is a digit, stands for
whatever matched the dth parenthesized grouping in regexp.
To include a `\' in the text to replace with, you must enter
`\\'. For example,
M-x replace-regexp RET c[ad]+r RET \&-safe RET
replaces (for example) `cadr' with `cadr-safe' and `cddr' with `cddr-safe'.
M-x replace-regexp RET \(c[ad]+r\)-safe RET \1 RET
performs the inverse transformation.