我制作的正则表达式从 opts.taskDetails.order_details 中获取项目列表。
我想优化正则表达式/任何其他更好的正则表达式以获取以下数据,如上图所示。 (可以是1个项目列表,也可以是多个)。
除了获取项目详细信息外,我还想获取以下 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 Insomnio Especial \nS/212 S/23.12 *Tipo de papa:* - Papa 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 :(得分:0)
这里是regex
,用于切碎order_details
,并在末尾提取其余部分。
我使用一个rx来获取标题,项目和其他内容。然后另一个使用g
标志来解析项目。您可以在正则表达式中进行更多验证,或者将匹配项解析为数字,但您应该明白这一点。关键是两阶段解析。
我将结果放入order
对象中。您可以简单地将其映射到所需的任何内容。显示与其他代码隔离的正则表达式和解决方案更加容易。
var order_details = `Detalle\n Unidad\n Cantidad\n Total\n Triple Insomnio Especial S/37.90 1 S/37.90 Insomnio Especial \nS/23.12 1 S/23.12 Tipo de papa: - Papa 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 order_details2 = `Detalle\n Unidad\n Cantidad\n Total\n Combo Cheese Burger para 2 S/39.90 1 S/39.90 Salsas primera: -\nGolf S/0.00\n Elige tu bebida: - Coca Cola 500ml S/0.00\n Elige tu bebida: - Coca Cola 500ml S/0.00\n Verduras primera : - Cebolla blanca S/0.00\n - Tomate S/0.00\n - Lechuga S/0.00\n Salsas segunda : - Golf S/0.00\n - Tártara S/0.00\n Verduras segunda : - Cebolla blanca S/0.00\n - Tomate S/0.00\n - Lechuga S/0.00\n Porción de papa : - Papa Regular S/0.00\n`
function parse(string) {
// header, items, and tipo
var rxItems = /Detalle\n Unidad\n Cantidad\n\ Total\n\s*([^:]+)(([^:]+:[\s\S]*)) /
// single item line
var rxItem = /s*(.*?)\s+(S\/.*?)\s+(\d+)\s+(S\/.*?)/g
var order = {
items: [],
other: null
}
var mParts = rxItems.exec(string)
var items = mParts[1]
order.other = mParts[2]
var mItem = rxItem.exec(items)
while (mItem) {
var detalle = mItem[1]
var unidad = mItem[2]
var cantidad = mItem[3]
var total = mItem[4]
let item = {
detalle,
unidad,
cantidad,
total
}
order.items.push(item)
mItem = rxItem.exec(items)
}
console.log(order)
}
parse(order_details)
parse(order_details2)