I have a input string and a replacement object, Now have to find occurrence of a text ( object key) separated by = and want to replace with object values. but unable to find the solution. Here is my snippet and the code is as follow.
var replacer = function(tpl, data) {
console.log('input==>', tpl);
for(var key in data) {
var re = new RegExp(key + '=(.*)', 'g');
tpl = tpl.replace(re, function(match, p1, offset, string) {
console.log('Replace=>', arguments);
return p1;
});
console.log('output==>', tpl);
}
console.log('final output==>', tpl);
};
var text = 'alpha=1\nbeta=2\nage=12\ncolor=green';
var result = replacer(text, { age: 15, color: 'red' });
input string : 'alpha=1\nbeta=2\nage=12\ncolor=green'
replacer object : { age: 15, color: 'red' }
desired output is : 'alpha=1\nbeta=2\nage=15\ncolor=red'
Trial 1
use return p1;, then final output was ==> 'alpha=1\nbeta=2\n12\ngreen'
Trial 2
use return data[key]; than final output was ==> 'alpha=\nbeta=2\n15\nred'
So What will be the right step to achieve the desire output ?
tplbut it is not what I want.