Skip to content

Instantly share code, notes, and snippets.

@qutek
Created February 9, 2023 05:30
Show Gist options
  • Select an option

  • Save qutek/d75afedab924278de925267d8f8d10dc to your computer and use it in GitHub Desktop.

Select an option

Save qutek/d75afedab924278de925267d8f8d10dc to your computer and use it in GitHub Desktop.

Revisions

  1. qutek created this gist Feb 9, 2023.
    10 changes: 10 additions & 0 deletions replace.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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;
    }
    };