Find and replace string

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 with 11/12, ie remove the measure in CM and the hyphen before.

Any tips for regex?

    
asked by anonymous 14.11.2016 / 13:04

1 answer

2

Substitution can be done with the regular expression / *- *[0-9]+ *CM/g where * means zero ou mais spaces and [0-9]+ means 1 ou mais digits. So we can transform JSON into 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 substitute based on the certainty that the object will be in the label attribute within the attributes options:

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));
    
14.11.2016 / 13:26