const regex = /\${[^{]+}/g; export default function interpolate(template, variables, fallback) { return template.replace(regex, (match) => { const path = match.slice(2, -1).trim(); return getObjPath(path, variables, fallback); }); } //get the specified property or nested property of an object function getObjPath(path, obj, fallback = '') { return path.split('.').reduce((res, key) => res[key] || fallback, obj); }