获取JavaScript中两个字符串之间的文本(Google脚本)

时间:2019-03-23 10:02:50

标签: javascript regex string

我从JavaScript中从凭证(文本为变量名)中提取了以下JSON.stringify(text):

  

“ \ n车辆详细信息\ n乘客详细信息\ n经济型车\ n最大   乘客4 \ n行李箱容量4 \ n名字\ nLeif \ n电子邮件   \ nBR@dRI.com \ n姓氏\ n姓氏\ nBeermer \ nBeermer \ nMobile   电话号码\ n46712 125313 \ n46712 125123 \ n旅客\ n成人1   \ n儿童0 \ n婴儿0 \ n其他选项\ nno_extras_in_voucher   \ n付款\ n付款方式信用卡\ n已付款60€\ nAmount   等待中0€\ n到达\ n下车地点Divani Palace Acropolis   \ n航班到达时间12:55 AM \ n航空公司SsS \ n航班号SK717   \ n出发机场(您的航班从哪里来?)哥本哈根\ n接机   位置雅典机场\ n返回\ n返回\ n下车位置雅典   机场\ n下车地点雅典机场\ n航班起飞时间   13:45 \ n航班起飞时间13:45 \ n航空公司SAS \​​ n航空公司SAS   \ n航班号SK778 \ n航班号SK778 \ n从您的接机时间   住宿11:00 AM \ n从您的住宿中选择接送时间11:00 AM   \ n从您的住宿处取车时间11:00 AM \ n取车地点   Divani Palace Acropolis \ n接送地点Divani Palace Acropolis   \ n预订代码:7777预订日期:22/03/2019 09:22总费用:60€   \ n到达航班日期和时间28/03/2019 \ n住宿名称Divani   雅典卫城皇宫\ n住宿地址Parthenonos 19,Athina 117   42,希腊\ n评论\ n航班出发日期29/03/2019   \ n住宿名称Divani Palace雅典卫城\ n住宿地址   希腊Parthenonos 19,雅典娜117 42 \ n评论

我会得到粗体字。未加粗的单词是固定的。即,除了粗体字以外,每个凭单都具有相同的确切格式。正如您所看到的,有很多重复的单词,而且其中有些可能是两个或什至三个单词(例如,经济舱或酒店阿姆斯特丹)。我现在正在做的是尝试在两个字符串之间获取文本。例如,如果我想获取文本 经济车,我会使用以下正则表达式:

text.match(/Details ([\s\S]*?) Maximum/)

但是这返回None,我认为这是因为字符串中有很多值或重复的单词。我想避免for循环,因为我使用的是Google脚本,并且存在运行时限制。我应该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

更新:更新了代码以与Apps脚本一起使用 假设您需要一个脚本来解析多个相似的字符串。假设只有粗体的文本发生变化。

基本算法是从结尾开始并通过fireld解析字段。 您将需要一个字段名称数组:

var fields = [
"Vehicle Details Passenger Details",
"Maximum Passengers",
//...
"Airline",
"Airline SEK Flight Number"
]

然后假设您的字符串为str值,则执行循环:

var values = [];
for(var i = fields.length - 1; i > -1; i--){
  var indexOfField = str.lastIndexOf(fields[i]);
  var fieldLength = fields[i].length;
  var value = str.substr(indexOfField + fieldLength);
  values.push(value);
  str = str.substr(0, indexOfField);
}
Logger.log(values)

答案 1 :(得分:-1)

文本看起来像是最初是HTML的文本表示形式。这可能意味着某些空格字符是其他空格,例如TAB或换行符。因此,最好在正则表达式中使用\s+。附带说明:如果可以访问HTML,则最好依靠HTML而不是文本表示形式。

您可以列出字段标签并获取它们之间出现的文本。需要一些额外的逻辑来忽略空值,重复值或跳过可能丢失的标签,而不会破坏其余的过程。

此过程仍然很大程度上取决于您所说的假设:

  

未加粗的单词是固定的。即,每张优惠券都具有相同的确切格式

此代码产生字段/值对。因为字段(因为它们出现在输入中)不是唯一的,所以结果被放置在数组中,而不是在由字段标签作为键的对象中:

// Input data
var text = " \nVehicle Details \nPassenger Details \nEconomy Car \nMaximum Passengers 4 \nSuitcases capacity 4 \nFirst Name \nTerf \nEmail \nBdeONT@gmail.com \nLast Name \nLast Name \nNick \nNick \nMobile Phone Number \n43702 136 845 \n43702 136 845 \nPassengers \nAdults 2 \nChildren 0 \nInfants 0 \nAdditional Options \nno_extras_in_voucher \nPayment \nPayment Method Credit Card \nAmount Paid 60 € \nAmount pending 0 € \nArrival \nDrop off Location Hotel Acropolis \nFlight Arrival Time 12:55 AM \nAirline SKG \nFlight Number SK732 \nOriginating Airport (Where your flight is from?) Amsterdam \nPickup Location Athens Airport \nReturn \nReturn \nDrop-Off Location Athens Airport \nDrop-Off Location Athens Airport \nFlight Departure Time 13:45 \nFlight Departure Time 13:45 \nAirline SKG \nAirline SKG \nFlight Number SK732 \nFlight Number SK732 \nPick Up Time From Your Accommodation 11:00 AM \nPick Up Time From Your Accommodation 11:00 AM \nPick Up Time From Your Accommodation 11:00 AM \nPick Up Location Hotel Acropolis \nPick Up Location Hotel Acropolis \nBooking Code: 744 Booking Date: 22/03/2019 09:22 Total Cost: 60 € \nArrival Flight Date & Time 28/03/2019 \nAccommodation Name Hotel Acropolis \nAccommodation Address Parth 11, Athina 117 42, Greece \nComments \nFlight Departure Date 29/03/2019 \nAccommodation Name Hotel Acropolis \nAccommodation Address Parthen 19, Athina 117 42, Greece \nComments "

var fields = [
  "Vehicle Details", "Passenger Details", "Maximum Passengers", 
  "Suitcases capacity", "First Name", "Email", "Last Name", 
  "Last Name", "Mobile Phone Number", "Passengers", "Adults", 
  "Children", "Infants", "Additional Options", "Payment", 
  "Payment Method", "Amount Paid", "Amount pending", "Arrival", 
  "Drop off Location", "Flight Arrival Time", "Airline",
  "Flight Number", "Originating Airport (Where your flight is from?)",
  "Pickup Location", "Return", "Return", "Drop-Off Location",
  "Drop-Off Location", "Flight Departure Time",
  "Flight Departure Time", "Airline", "Airline", "Flight Number",
  "Flight Number", "Pick Up Time From Your Accommodation",
  "Pick Up Time From Your Accommodation",
  "Pick Up Time From Your Accommodation",
  "Pick Up Location", "Pick Up Location", "Booking Code:", 
  "Booking Date:", "Total Cost:", "Arrival Flight Date & Time",
  "Accommodation Name", "Accommodation Address",
  "Comments", "Flight Departure Date", "Accommodation Name",
  "Accommodation Address", "Comments"
];

var result = fields.reduceRight(function (acc, field, j) {
    var i = acc[0].lastIndexOf(field);
    var value = acc[0].slice(i+field.length).trim().split("\n")[0].trim();
    return [acc[0].slice(0, i), 
            i<0 || !value || field==fields[j+1] 
                ? acc[1] 
                : [{ field: field, value: value }].concat(acc[1])];
}, [text, []]).pop();

console.log(result);

输出结构是一个对象数组,其中每个对象都有一个字段和值属性。这意味着您需要遍历数组以查找特定字段。如果输出是一个纯对象,您可以在其中通过键访问值,那会更好。问题在于字段不是唯一的(例如“航班号”)。

这里是一种替代解决方案,其中这些字段将获得一个值数组:

// Input data
var text = " \nVehicle Details \nPassenger Details \nEconomy Car \nMaximum Passengers 4 \nSuitcases capacity 4 \nFirst Name \nTerf \nEmail \nBdeONT@gmail.com \nLast Name \nLast Name \nNick \nNick \nMobile Phone Number \n43702 136 845 \n43702 136 845 \nPassengers \nAdults 2 \nChildren 0 \nInfants 0 \nAdditional Options \nno_extras_in_voucher \nPayment \nPayment Method Credit Card \nAmount Paid 60 € \nAmount pending 0 € \nArrival \nDrop off Location Hotel Acropolis \nFlight Arrival Time 12:55 AM \nAirline SKG \nFlight Number SK732 \nOriginating Airport (Where your flight is from?) Amsterdam \nPickup Location Athens Airport \nReturn \nReturn \nDrop-Off Location Athens Airport \nDrop-Off Location Athens Airport \nFlight Departure Time 13:45 \nFlight Departure Time 13:45 \nAirline SKG \nAirline SKG \nFlight Number SK732 \nFlight Number SK732 \nPick Up Time From Your Accommodation 11:00 AM \nPick Up Time From Your Accommodation 11:00 AM \nPick Up Time From Your Accommodation 11:00 AM \nPick Up Location Hotel Acropolis \nPick Up Location Hotel Acropolis \nBooking Code: 744 Booking Date: 22/03/2019 09:22 Total Cost: 60 € \nArrival Flight Date & Time 28/03/2019 \nAccommodation Name Hotel Acropolis \nAccommodation Address Parth 11, Athina 117 42, Greece \nComments \nFlight Departure Date 29/03/2019 \nAccommodation Name Hotel Acropolis \nAccommodation Address Parthen 19, Athina 117 42, Greece \nComments "

var fields = [
  "Vehicle Details", "Passenger Details", "Maximum Passengers", 
  "Suitcases capacity", "First Name", "Email", "Last Name", 
  "Last Name", "Mobile Phone Number", "Passengers", "Adults", 
  "Children", "Infants", "Additional Options", "Payment", 
  "Payment Method", "Amount Paid", "Amount pending", "Arrival", 
  "Drop off Location", "Flight Arrival Time", "Airline",
  "Flight Number", "Originating Airport (Where your flight is from?)",
  "Pickup Location", "Return", "Return", "Drop-Off Location",
  "Drop-Off Location", "Flight Departure Time",
  "Flight Departure Time", "Airline", "Airline", "Flight Number",
  "Flight Number", "Pick Up Time From Your Accommodation",
  "Pick Up Time From Your Accommodation",
  "Pick Up Time From Your Accommodation",
  "Pick Up Location", "Pick Up Location", "Booking Code:", 
  "Booking Date:", "Total Cost:", "Arrival Flight Date & Time",
  "Accommodation Name", "Accommodation Address",
  "Comments", "Flight Departure Date", "Accommodation Name",
  "Accommodation Address", "Comments"
];

var result = fields.reduceRight(function (acc, field, j) {
    var i = acc[0].lastIndexOf(field);
    var value = acc[0].slice(i+field.length).trim().split("\n")[0].trim();
    var text = acc[0].slice(0, i);
    if (i<0 || !value || field==fields[j+1]) return [text, acc[1]];
    acc[1][field] = field in acc[1] ? [].concat(acc[1][field], value) : value;   
    return [text, acc[1]];
}, [text, {}]).pop();

console.log(result);

现在,您可以按以下方式获取“航班起飞日期”:

console.log(result["Flight Departure Date"]);