function remapUrl({ input, mapFrom, mapTo }: { input: string, mapFrom: string, mapTo: string}): string | undefined { const partsInput = input.split('/'); const partsMapFrom = mapFrom.split('/'); const partsMapTo = mapTo.split('/'); if(partsInput.length == partsMapFrom.length) { for(let i=0; i -1) { const partMapTo = partsMapTo[indexMapTo]; if(partMapTo === partMapFrom) { partsMapTo[indexMapTo] = partInput; continue; } } return; } } return partsMapTo.join("/"); } } const remap: string | undefined = remapUrl({ input: "/base/theproj/app01", mapFrom: "/base/:project/:id", mapTo: "https://vcs.com/remote/raw/:project/show/:id" }); console.log(remap); // Output: "https://vcs.com/remote/raw/theproj/show/app01"