1. Solution 01 const replaceClose = ( index, string, close, replace, head = string.substring(0, index) + replace, tail = string.substring(index + close.length), next = tail.indexOf(close) ) => head + (next < 0 ? tail : replaceClose(next, tail, close, replace)) ) => { let result = ''; do { const head = string.substring(0, index) + replace; string = string.substring(index + close.length); index = string.indexOf(close); result += head; } while (index >= 0); return result + string; } 2. Solution 02 const replaceClose = ( index, string, close, replace, head = string.substring(0, index) + replace, tail = string.substring(index + close.length), next = tail.indexOf(close) ) => { console.log(index, string, close, replace); return head + (next < 0 ? tail : replaceClose(next, tail, close, replace)); }