4

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 ?

2
  • 1
    Your replacer function doesn't return anything Commented Dec 14, 2016 at 13:25
  • yes, but I have checked the final output tpl but it is not what I want. Commented Dec 14, 2016 at 13:27

3 Answers 3

2

Here is a variation of the code that also deals with those cases where data[key] is undefined. Note you do not need to use any capturing groups in the pattern, and thus you may shorten the list of arguments sent to the anonymous callback method:

var replacer = function(tpl, data) {
  for(var key in data) {
    var re = new RegExp(key + '=.*', 'g');
    tpl = tpl.replace(re, data[key] ? key + '=' + data[key] : '$&');
  }
  return tpl;
};

var text = 'alpha=1\nbeta=2\nage=12\ncolor=green';
var result = replacer(text, { age: 15, color: 'red' });
console.log(result);

Another point: if you do not want to match age key in mage, you need to use

var re = new RegExp('\\b' + key + '=.*', 'g');
                     ^^^^  
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the \b tips. and yes this is better optiong using without capturing groups.
Actually, on second thought, you may get rid of the anonymous method since the arguments are no longer used. I updated the answer to make the solution more compact.
this is awesome, Kindly explain what and how $& work here?
$& is the backreference to the whole match value. When you had an anonymous callback method, you used match variable to refer to the whole match. When using string replacement patterns, the same is expressed with $& backreference. In the latest versions of Chrome and FF you can even use PCRE-like $0, but it does not work in IE for the time being.
2

The replacement function has to return something to replace the entire match

return key+'='+data[key];

You also need to return tpl from your function

3 Comments

Yes, if data[key] is not undefined.
does this prevent unmatched data;
If you replace return p1 in your existing code with Juan's code, the replacement is performed only for the specific key from the for..in loop. In which case key is sure to be a property of data and unmatched data shouldn't be a problem.
1

You can try also

var replacer = function(tpl, data) {
  console.log('input==>', tpl);
  for(var key in data) {
    var re = new RegExp(key + '=(.*)', 'g');
    tpl = tpl.replace(re, key+'='+data[key]);
  }
   console.log('final output==>', tpl);
};

var text = 'alpha=1\nbeta=2\nage=12\ncolor=green';

var result = replacer(text, { age: 15, color: 'red' });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.