How can I change a string into new string if it has any placeholders with square brackets inside curly brackets in javascript?

I get value for parameterisedString from database and parameterisedString contains some placeholders with no square brackets inside curly bracket e.g {Group} and some placeholders with square brackets inside curly brackets e.g {termMonth [month]} in the same string.

Example:
parameterisedString='{Group} {Desc} – {termMonth [month]} – {termOdometer [kms]} {[$] DeductibleAmount [version]} {DeductibleType}’;

Placeholder with just curly bracket(e.g {Group}) doesn’t need to be replaced or changed but every Placeholder with square bracket inside curly bracket (e.g {termMonth [month]}) should be converted into {termMonth} placeholder and the text inside square bracket should be passed to getTranslation function to get Translation for that word and should not be interpolated .
e.g {termMonth [month]} : {termMonth} should be replaced or changed to {termMonth} and “month” should be replaced with Translation for that word.

The desired should be now:
parameterisedString = ‘{Group} {Desc} – {termMonth} mois – {termOdometer} kms USD$ {DeductibleAmount} couverture {DeductibleType}’;

buildTitle(dealProduct: DealProductModel, tittleSuffix: boolean = false) {
    //Example:
    let parameterisedString='{Group} {Desc} - {termMonth [month]} - {termOdometer [kms]} {[$] DeductibleAmount [version]} {DeductibleType}';

    const pairs = [
        ['{Group}', `${dealProduct.name}`],
        ['{Desc}', `${dealProduct.coverageName}`],
        ['{termMonth}', `${dealProduct.termMonths}`],
        ['{termOdometer}', `${dealProduct.termOdometer.toLocaleString()}`],
        ['{DeductibleAmount}', `${dealProduct.deductibleAmount} Deductible`],
        ['{DeductibleType}', `${dealProduct.deductibleType}`]
      ];
              
      //HERE I NEED the changed parameterisedString and it should have all the words translated in square brackets before I interpolate. The desired should be now:     
      
      parameterisedString = '{Group} {Desc} - {termMonth} mois - {termOdometer} kms USD$ {DeductibleAmount} couverture {DeductibleType}';
      
      // interpolate the string
        let key, val;
        for (let i = 0; i < pairs.length; i++) {
          [key, val] = pairs[i];
          parameterisedString = parameterisedString.replace(`${key}`, `${val}`);
        }

    return parameterisedString;
  }


getTranslation(word: string) {  
    -----
    -----
    return  TranslatedWord; 
}

the desired should be now:
parameterisedString = ‘{Group} {Desc} – {termMonth} mois – {termOdometer} kms USD$ {DeductibleAmount} couverture {DeductibleType}’;

70 thoughts on “How can I change a string into new string if it has any placeholders with square brackets inside curly brackets in javascript?”