Find and replace string

Asked

Viewed 60 times

1

I have this json returned by a webservice:

spConfig[92769] = new Product.Config({"attributes":{"152":{"id":"152","code":"color","label":"Cor","options":[{"id":"1777","label":"AZUL-INDIGO","price":"0","oldPrice":"0","products":["92781","92782","92784","92785","92786"]},{"id":"1775","label":"AZUL-MEDIO","price":"0","oldPrice":"0","products":["92795","92796","92797","92798","92799"]}]},"153":{"id":"153","code":"size","label":"Tamanho","options":[{"id":"1807","label":"3\/4 - 104CM","price":"0","oldPrice":"0","products":["92781"]},{"id":"1808","label":"5\/6","price":"0","oldPrice":"0","products":["92782","92795"]},{"id":"1809","label":"7\/8","price":"0","oldPrice":"0","products":["92796"]},{"id":"1810","label":"9\/10","price":"0","oldPrice":"0","products":["92784","92797"]},{"id":"1921","label":"11\/12 - 152CM","price":"0","oldPrice":"0","products":["92785","92798"]},{"id":"1922","label":"13\/14","price":"0","oldPrice":"0","products":["92786","92799"]}]},"168":{"id":"168","code":"leg_size","label":"Tamanho de Perna","options":[{"id":"1785","label":"00","price":"0","oldPrice":"0","products":["92781","92782","92784","92785","92786"]},{"id":"1881","label":"20","price":"0","oldPrice":"0","products":["92795","92796","92797","92798","92799"]}]}},"template":"\u20ac\u00a0#{price}","basePrice":"17.99","oldPrice":"17.99","productId":"92769","chooseText":"Seleccione una opci\u00f3n...","taxConfig":{"includeTax":false,"showIncludeTax":false,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Impuestos incluidos"},"defaultValues":{"152":"1777","153":"1807","168":"1785"}});

I need to find, for example, 11/12 - 152CM and replace by 11/12, that is, remove the measure in CM and the hyphen, before.

Any hint for regex?

  • 1

    It is only in the attribute "label"?

1 answer

2

Replacement can be done with regular expression / *- *[0-9]+ *CM/g where * that is to say zero ou mais spaces and [0-9]+ that is to say 1 ou mais digits. So we can transform JSON into a string to do the direct substitution as follows:

var config = {"attributes":{"152":{"id":"152","code":"color","label":"Cor","options":[{"id":"1777","label":"AZUL-INDIGO","price":"0","oldPrice":"0","products":["92781","92782","92784","92785","92786"]},{"id":"1775","label":"AZUL-MEDIO","price":"0","oldPrice":"0","products":["92795","92796","92797","92798","92799"]}]},"153":{"id":"153","code":"size","label":"Tamanho","options":[{"id":"1807","label":"3\/4 - 104CM","price":"0","oldPrice":"0","products":["92781"]},{"id":"1808","label":"5\/6","price":"0","oldPrice":"0","products":["92782","92795"]},{"id":"1809","label":"7\/8","price":"0","oldPrice":"0","products":["92796"]},{"id":"1810","label":"9\/10","price":"0","oldPrice":"0","products":["92784","92797"]},{"id":"1921","label":"11\/12 - 152CM","price":"0","oldPrice":"0","products":["92785","92798"]},{"id":"1922","label":"13\/14","price":"0","oldPrice":"0","products":["92786","92799"]}]},"168":{"id":"168","code":"leg_size","label":"Tamanho de Perna","options":[{"id":"1785","label":"00","price":"0","oldPrice":"0","products":["92781","92782","92784","92785","92786"]},{"id":"1881","label":"20","price":"0","oldPrice":"0","products":["92795","92796","92797","92798","92799"]}]}},"template":"\u20ac\u00a0#{price}","basePrice":"17.99","oldPrice":"17.99","productId":"92769","chooseText":"Seleccione una opci\u00f3n...","taxConfig":{"includeTax":false,"showIncludeTax":false,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Impuestos incluidos"},"defaultValues":{"152":"1777","153":"1807","168":"1785"}};
var str = JSON.stringify(config);

str = str.replace(/ *- *[0-9]+ *CM/g, '');
config = JSON.parse(str);
console.log(JSON.stringify(config));

Or we can replace based on the certainty that the object will be in the attribute label within the options of attributes:

var config = {"attributes":{"152":{"id":"152","code":"color","label":"Cor","options":[{"id":"1777","label":"AZUL-INDIGO","price":"0","oldPrice":"0","products":["92781","92782","92784","92785","92786"]},{"id":"1775","label":"AZUL-MEDIO","price":"0","oldPrice":"0","products":["92795","92796","92797","92798","92799"]}]},"153":{"id":"153","code":"size","label":"Tamanho","options":[{"id":"1807","label":"3\/4 - 104CM","price":"0","oldPrice":"0","products":["92781"]},{"id":"1808","label":"5\/6","price":"0","oldPrice":"0","products":["92782","92795"]},{"id":"1809","label":"7\/8","price":"0","oldPrice":"0","products":["92796"]},{"id":"1810","label":"9\/10","price":"0","oldPrice":"0","products":["92784","92797"]},{"id":"1921","label":"11\/12 - 152CM","price":"0","oldPrice":"0","products":["92785","92798"]},{"id":"1922","label":"13\/14","price":"0","oldPrice":"0","products":["92786","92799"]}]},"168":{"id":"168","code":"leg_size","label":"Tamanho de Perna","options":[{"id":"1785","label":"00","price":"0","oldPrice":"0","products":["92781","92782","92784","92785","92786"]},{"id":"1881","label":"20","price":"0","oldPrice":"0","products":["92795","92796","92797","92798","92799"]}]}},"template":"\u20ac\u00a0#{price}","basePrice":"17.99","oldPrice":"17.99","productId":"92769","chooseText":"Seleccione una opci\u00f3n...","taxConfig":{"includeTax":false,"showIncludeTax":false,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Impuestos incluidos"},"defaultValues":{"152":"1777","153":"1807","168":"1785"}};
var str = JSON.stringify(config);

str = str.replace(/ *- *[0-9]+ *CM/g, '');
config = JSON.parse(str);
console.log(JSON.stringify(config));

Browser other questions tagged

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