title: Javascript to replace placeholder variable values
body: <p>I am trying to replace a string which has place holders with values in an object </p>
  
  <p>For Example: </p>
  
  <pre><code>let contentString = `
      I, &lt;&lt;firstName&gt;&gt; &lt;&lt;lastName&gt;&gt;, born on &lt;&lt;DOB&gt;&gt; of &lt;&lt;flatNumber&gt;&gt; , &lt;&lt;houseNumber&gt;&gt;
  `;
  
  
  let data = {
    firstName: 'test_firstName',
    lastName: 'test_lastName',
    DOB: '10-12-1987',
    flatNumber: '3',
    houseNumber: '8'
  };
  
  function createTemplate(contentString, data) {
    let pattern = /&lt;&lt;(\w*?)&gt;&gt;/gm;
  
  
    while (matches = pattern.exec(contentString)) {
  
      contentString.replace(matches[1], function replacer(match) {
        console.log(match);
        //contentString = contentString.replace(match, data[match]);
      });
  
    }
  
    console.log(contentString);
  
  
  }
  
  createTemplate(contentString, data);
  </code></pre>
  
  <p>The console.log function in the replacer callback function extracts all the values such as firstName,lastName,DOB,flatNumber and houseNumber.</p>
  
  <p>However, once i uncomment the next line of code which is string replace although the firstName is replaced by the value the <code>&lt;&lt;lastName&gt;&gt;</code> is never replaced.</p>
  
  <p>Whereas if i change the contentString to something like </p>
  
  <pre><code>let contentString = `
      I, &lt;&lt;firstName&gt;&gt; asnasas  &lt;&lt;lastName&gt;&gt;, born on &lt;&lt;DOB&gt;&gt; of &lt;&lt;flatNumber&gt;&gt; , &lt;&lt;houseNumber&gt;&gt;
  `;
  </code></pre>
  
  <p>inserting some text between the <code>&lt;&lt;firstName&gt;&gt;</code> and <code>&lt;&lt;lastName&gt;&gt;</code> it works.</p>
  
  <p>Can anyone please guide is towards my mistake. Thank you in advance.</p>