0
I’m trying to do the replace
of a string, similar to the one in the example, in it I have wildcards ${}
and within it I have those who would be the key to an object for substitution.
Given this explanation, what would be the best option to make this replacement ?
let string = 'PP24,260:AN7'+
'BARSET "QRCODE",1,1,11,2,1'+
'PB "${qrcode}"'+
'PP 270,265'+
'PT "${cliente}"'+
'PP 270,225'+
'PT "${comprimento}"'+
'PP 270,185'+
'PT "${diametro}"'+
'PP 270,145'+
'PT "${espessura}"'+
'PP 270,105'+
'PT "${aco}"'+
'PP 270,65'+
'PT "${peso}"'+
'PP 550,145'+
'PT "${lote}"'+
'PP 550,105'+
'PT "${ordem}"'+
'PP 550,65'+
'FT "Swiss 721 BT",11'+
'PT "${data}"'+
'LAYOUT RUN ""'+
'PF'+
'PRINT KEY OFF';
let obj = {
qrcode: "asd1231",
cliente: "1321384a684a654a564z6",
comprimento: "1256.2",
diametro: "13.5",
espessura: "26.5",
aco: "3695z",
peso: "165.00",
lote: "65zxc651z3x1",
ordem: "5as651651",
data: "2018-01-23"
};
console.log('res', string, obj)
I’m trying to do it this way
for(item in obj){
console.log(item, obj[item]);
console.log(string.replace(new RegExp('${'+item+'}', 'g'), obj[item]));
}
I go through all keys of the object and for each key I do the replace, but test way is not working, too :(
Yes, we have some problems with this approach. 1º - This string will be in the database. 2º - I need you to have " " because it is a string in the direct print language
– Renan
Ahh, put that detail in the question, makes all the difference then. And add the direct print tag, because without it the solution would be pure javascript, as I indicated.
– Giuliana Bezerra