1

I have a string like "today is %date%, my name is %firstName% %lastName%"

Using regular expression I want to replace the %date% with todays date, same goes for firstName and lastName

I did something like

  let reg = new RegExp(/\%(.*?)\%/g); // this gives me the values inside % 
    val = reg.exec(value);

How do I replace the existing string with the values.

EDIT: If I do something like str.replace((/\%(.*?)\%/g), 'test'); It does replace all the values with test how do I get the value that was replaced like:

today is %date%, my name is %firstName% %lastName%

today is {{date}}, my name is {{firstName}} {{lastName}}

How do I solve this problem?

Emma
  • 27,428
  • 11
  • 44
  • 69
Jay
  • 2,485
  • 6
  • 28
  • 34

0 Answers0