I have a text file like this:
// start file
var nameVar1 = '';
// var nameVar1 = '';
var otherVar1= "test1";
var nameVar2 ="test2",
otherVar2 = 'test3';
var nameVar3 = true;
var otherVar3 = false ;
var objName = {
test: 'test'
};
var expression = nameVar1 + nameVar2;
// comment
for(var test in templateConfig) {
if( templateConfig ){ defaultLang = langParam; }
}
// end file
I am trying to find all the variables that have fields and replace them with interpolation and get an example below:
// start file
{{nameVar1}}
// var nameVar1 = '';
{{otherVar1}}
{{nameVar2}}
{{otherVar2}}
{{nameVar3}}
{{otherVar3}}
{{objName}}
var expression = nameVar1 + nameVar2;
// comment
for(var test in templateConfig) {
if( templateConfig ){ defaultLang = langParam; }
}
// end file
I got a regular expression but it doesn't work very well because it also finds variables in loops or expressions
const regex = /(?<!\/\/\s*?)(?:var|let|const)(?:\s+)(.*?)(?:\s+)?=\s?(?:.*?);/gms;
const subst = `{{$1}}`;
const result = str.replace(regex, subst);