使用正则表达式获取项目列表和其他数据

时间:2018-09-09 08:37:30

标签: javascript regex regex-group

我制作的正则表达式从 opts.taskDetails.order_details 中获取项目列表。

enter image description here

我想优化正则表达式/另一种方式来获取如下图所示的数据。

除了获取项目详细信息外,我还想获取从 opts.taskDetails.order_details 获取项目列表之后剩余的数据。

Tipo de papa: -\nPapa Amarilla Tumbay S/0.00\n Adicional : - Quesp cheddar S/0.00\n Agrega Bebida: - Fanta 400 ml S/0.00\n
Salsas: - Mayonesa S/0.00\n - Ketchup S/0.00\n - Golf S/0.00\n - Guacamole S/0.00\n - Ají S/0.00\n - Tártara S/0.00\n - Mayo-aji S/0.00\n

var opts = {
      "items": [{
        "mail_parser_label": "order_details",
        "label": "order_details",
        "data_type": "Table",
        "app_side": "0",
        "required": 0,
        "value": 1,
        "data": {
          "head": [{
            "mail_parser_label": "Detalle",
            "label": "Detalle",
            "type": "text",
            "arth": " ",
            "id": 0,
            "show": 1,
            "app_side": 0,
            "required": 0
          }, {
            "mail_parser_label": "Cantidad",
            "label": "Cantidad",
            "type": "text",
            "arth": " ",
            "id": 1,
            "show": 1,
            "app_side": 0,
            "required": 0
          }, {
            "mail_parser_label": "Unidad",
            "label": "Unidad",
            "type": "text",
            "arth": " ",
            "id": 2,
            "show": 1,
            "app_side": 0,
            "required": 0
          }, {
            "mail_parser_label": "Total",
            "label": "Total",
            "type": "text",
            "arth": " ",
            "id": 2,
            "show": 1,
            "app_side": 0,
            "required": 0
          }],
          "other": [],
          "body": []
        },
        "template_id": "NUEVO_PEDIDO",
        "appCheck": true
      }],
      "taskDetails": {
        "order_details": "Detalle\n Unidad\n Cantidad\n Total\n     Triple Insomnio Especial   S/37.90 1  S/37.90    *Tipo de papa:*  -\nPapa Amarilla Tumbay S/0.00\n     *Adicional :*  - Quesp cheddar S/0.00\n     *Agrega Bebida:*  - Fanta 400 ml S/0.00\n     *Salsas:*  - Mayonesa S/0.00\n - Ketchup S/0.00\n - Golf S/0.00\n - Guacamole S/0.00\n - Ají S/0.00\n - Tártara S/0.00\n - Mayo-aji S/0.00\n      "
      }
    }
    
    var items = opts.items;
    var taskDetails = opts.taskDetails;
    for (var p = 0; p < items.length; p++) {
      var parameter = items[p];
      if (parameter.mail_parser_label == "order_details") {
        var str = taskDetails.order_details.toString().replace(/[ ]+/g, ' ');
        var itemPatternFull = str.match(/[A-Za-z0-9 ]*S\/\d{1,}[.]*\d*\s+\d{1,}\s+S\/\d{1,}[.]*\d*/g);
        var itemPattern = itemPatternFull ? itemPatternFull.toString().match(/S\/\d{1,}[.]*\d*\s+\d{1,}\s+S\/\d{1,}[.]*\d*/g) : [];
        var details = itemPatternFull ? itemPatternFull.toString().replace(/S\/\d{1,}[.]*\d*\s+\d{1,}\s+S\/\d{1,}[.]*\d*/g, '').toString().split(',') : [];
        var itemPatternLength = str.match(/S\/\d{1,}[.]*\d*\s+\d{1,}\s+S\/\d{1,}[.]*\d*/g).length;
        var itemPattern = str.match(/S\/\d{1,}[.]*\d*\s+\d{1,}\s+S\/\d{1,}[.]*\d*/g);
        var itemPatternArray = itemPattern.toString().replace(',', ' ').split(' ');
        str = str.split(' ');
    
    
    
        var count = 0;
        if (itemPatternArray.length < str.length && details.length == itemPatternArray.length / 3) {
          for (columnIndex = 0; columnIndex < itemPatternArray.length; columnIndex++) {
            if (columnIndex % 3 == 0) {
              parameter.data.body.push({
                "val": details[count].trim(),
                "id": count + columnIndex,
                "head": "Dettalle"
              });
              count++;
            }
            parameter.data.body.push({
              "val": itemPatternArray[columnIndex].replace(/S\//, ''),
              "id": columnIndex + count,
              "head": str[(columnIndex % 3) + 1] ? str[(columnIndex % 3) + 1].toString().replace('\n', '') : str[(columnIndex % 3) + 1]
            });
          }
        }
      } else {
        parameter.data = taskDetails[parameter.mail_parser_label].toString().trim().replace(/[\s+\n]/g, ' ');
      }
    }
    console.log("ITEMS", parameter.data.body)

0 个答案:

没有答案