Skip to content

Instantly share code, notes, and snippets.

@qutek
Created February 9, 2023 05:30
Show Gist options
  • Save qutek/d75afedab924278de925267d8f8d10dc to your computer and use it in GitHub Desktop.
Save qutek/d75afedab924278de925267d8f8d10dc to your computer and use it in GitHub Desktop.
[Replace Variable With Data] Simple variable template replacement like mustache / handlebars
const replacePlaceholder = (template, data) => {
try {
return template.replace(/{(\w+)}/g, (keyVariable, keyData) =>
data.hasOwnProperty(keyData) ? data[keyData] : variable
);
} catch (error) {
console.error('replacePlaceholder', error);
throw error;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment