#!/bin/bash
s='../foo
../../foo
../../../foo'
sed -E 's,(\.\./)+foo,foo1,' <<< "$s"