0

I am trying to replace a string which has place holders with values in an object

For Example:

let contentString = `
    I, <<firstName>> <<lastName>>, born on <<DOB>> of <<flatNumber>> , <<houseNumber>>
`;


let data = {
  firstName: 'test_firstName',
  lastName: 'test_lastName',
  DOB: '10-12-1987',
  flatNumber: '3',
  houseNumber: '8'
};

function createTemplate(contentString, data) {
  let pattern = /<<(\w*?)>>/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);

The console.log function in the replacer callback function extracts all the values such as firstName,lastName,DOB,flatNumber and houseNumber.

However, once i uncomment the next line of code which is string replace although the firstName is replaced by the value the <<lastName>> is never replaced.

Whereas if i change the contentString to something like

let contentString = `
    I, <<firstName>> asnasas  <<lastName>>, born on <<DOB>> of <<flatNumber>> , <<houseNumber>>
`;

inserting some text between the <<firstName>> and <<lastName>> it works.

Can anyone please guide is towards my mistake. Thank you in advance.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Anand
  • 121
  • 3
  • 12
  • 1
    If you are using `replace` with a replacer callback function anyway, why don't you simply use `return contentString.replace(pattern, …)` instead of using the loop and `exec()`? – Bergi Nov 23 '16 at 03:41
  • Not sure, but it sounds like a duplicate of [Replace method doesn't work](http://stackoverflow.com/q/1433212/1048572)? – Bergi Nov 23 '16 at 03:42
  • Why not just use a regular expression and replace `"<<"+key+">>"` with `value` for your `data` object? – Spencer Wieczorek Nov 23 '16 at 03:44
  • Your `while` loop does not work because you are changing `contentString` within the loop body, without adjusting `pattern.lastIndex`. – Bergi Nov 23 '16 at 03:45
  • Hi Thank you very much for the comments. But can you kindly please guide me with an example of using pattern.lastIndex – Anand Nov 23 '16 at 03:52
  • @SpencerWieczorek - Thank you for your comments. Not sure if i am moving in the right direction but if i have to implement your suggestion then let matches = contentString.replace(pattern,? ); what should i replace the ? with so that the key value suggestion would work – Anand Nov 23 '16 at 03:59
  • @Anand [Here is a fiddle](https://jsfiddle.net/cronueqz/) of what I mentioned. So you go though `data` with something like `for(key in data)` then do `contentString = contentString.replace("<<"+key+">>","g"), data[key])`. In the example I used a regexp so it replaces all values if there are many. – Spencer Wieczorek Nov 23 '16 at 05:00
  • @SpencerWieczorek - Thank you very much for your time and update. really appreciate your help – Anand Nov 23 '16 at 20:46

0 Answers0