export async function replaceAsync( str: string, regex: RegExp, asyncFn: (match: string, ...args: any[]) => Promise ) { const promises: Promise[] = []; str.replace(regex, (match: string, ...args: any[]): string => { const promise = asyncFn(match, ...args); promises.push(promise); return ""; }); const data = await Promise.all(promises); return str.replace(regex, () => data.shift()!); }