I believe this should solve your problem. I may be missing a few edge cases, please comment if you notice one.
You need a way to exclude previous substitutions from future patterns, which really means making outputs distinguishable, as well as excluding these outputs from your searches, and finally making outputs indistinguishable again. This is very similar to the quoting/escaping process, so I'll draw from it.
s/\\/\\\\/g
escapes all existing backslashess/ab/\\b\\c/g
substitutes raw ab for escaped bcs/bc/\\a\\b/g
substitutes raw bc for escaped abs/\\\(.\)/\1/g
substitutes all escaped X for raw XI have not accounted for backslashes in ab or bc, but intuitively, I would escape the search and replace terms the same way - \
now matches \\
, and substituted \\
will appear as \
.
Until now I have been using backslashes as the escape character, but it's not necessarily the best choice. Almost any character should work, but be careful with the characters that need escaping in your environment, sed, etc. depending on how you intend to use the results.