Perl, 3434 38 + 1 bytes
to handle the two cases
s,(/)|.,$"x($1?$c&&--$c:$c++).$&.$/,ge
to be run with -p
option
s,(/)|.,$"x($1?--$c:$c++).$&.$/,ge
EDIT: following comment doesn't work when first character is /
s,(/)|.,$"x($1?$c--:++$c).$&.$/,ge
however output will be shifted one character on the right if first char is \