Replace string with items from an object

Asked

Viewed 177 times

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 :(

2 answers

0


You can use a function as the second parameter of String.replace().

She will receive as arguments:

  • match: the married string in Regexp
  • g1, g2, g3, ...: the cadate groups in Regexp, may be 0 to N, depends only on your Regexp.
  • offset: the position in the string where the substring that matches Regexp was found
  • string: the full string

Then a solution could be, create a Regexp that marries with ${...}, group what is inside the keys, test if it exists in the object and replace the contents.

Ex.:

string.replace(/\${(.*?)}/g, (match, group, offset, string) => {
    return group in obj ? obj[group] : match
})

The regex will marry ${...} always grouping what is in ... and inside the function testo if there is the key in the object. If there is replaced, it keeps the same string.


Example running with your code:

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"
};

let x = string.replace(/\${([^}]*?)}/g, (match, group, offset, string) => {
  console.log(`
    match: ${match}
    group: ${group}
    return: ${group in obj ? obj[group] : match}
  `);
  return group in obj ? obj[group] : match
})

console.log(x)

0

You can do this natively using strings template Javascript. For this, you will change the ' of the string by ` and will directly access the properties of your object within the string template:

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"
    };

let string = `PP24,260:AN7
    BARSET "QRCODE",1,1,11,2,1
    PB ${obj.qrcode}
    PP 270,265
    PT ${obj.cliente}
    PP 270,225
    PT ${obj.comprimento}
    PP 270,185
    PT ${obj.diametro}
    PP 270,145
    PT ${obj.espessura}
    PP 270,105
    PT ${obj.aco}
    PP 270,65'+
    PT ${obj.peso}
    PP 550,145
    PT ${obj.lote}
    PP 550,105
    PT ${obj.ordem}
    PP 550,65
    FT "Swiss 721 BT",11
    PT ${obj.data}
    LAYOUT RUN ""
    PF
    PRINT KEY OFF`;

console.log('res', string, obj);

Note that it is very elegant to create the string, as it is no longer necessary to worry about concatenating it and replacing variable value markers.

  • 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

  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.