https://site.com/question/new-post/ должно получится https://site.com/question//.
const replacement = '!!!';.const newStr = str.replace(/(?<=\/)[^\/]*(?=\/[^\/]*$)/, replacement);
// или
const newStr = str
.split('/')
.map((n, i, a) => i && i === a.length - 2 ? replacement : n)
.join('/');
// или
const i = str.lastIndexOf('/');
const j = i > 0 ? -~str.lastIndexOf('/', ~-i) : 0;
const newStr = j ? str.slice(0, j) + replacement + str.slice(i) : str;