let ipt = '${abc} 8jfj ${def} opsdjl'; const vars = ipt.match(/\$\{.*?\}/g); const res = vars.reduce((fin, item) => (fin[item] = prompt(item), fin), {}); console.log(ipt.replace(/\$\{.*?\}/g, w => res[w]));