在javascript(google脚本)中获取两个字符串之间的文本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在javascript(google脚本)中获取两个字符串之间的文本相关的知识,希望对你有一定的参考价值。
我在javascript中从凭证中提取了以下JSON.stringify(文本)(文本是变量名称):
“\ n车辆详细信息\ n乘客详细信息\ n经济车\ n最大乘客4 \ n手提箱容量4 \ n第一名\ nLeif \ nEmail \ nBR@dRI.com \ n姓名\ n最后名称\ nBeermer \ nBeermer \ n移动电话号码\ n46712 125 313 \ n46712 125 123 \ nPassengers \ n成人1 \ n儿童0 \ n婴儿0 \ n其他选项\ nno_extras_in_voucher \ nPayment \ n付款方式信用卡\ n已支付60€\ n已挂起0€\ n到达\ nDrop off位置Divani Palace Acropolis \ nFlight抵达时间12 :55 AM \ n航空公司Ss \ n航班号SK717 \ nOriginating Airport(您的航班来自哪里?)哥本哈根\ nPickup位置雅典机场\ n返回\ n返回\ nDrop-Off位置雅典机场\ nDrop-Off位置雅典机场\ nFlight出发时间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 \ nPic k Up位置Divani Palace Acropolis \ nPick Up位置Divani Palace Acropolis \ n代号:7777预订日期:22/03/2019 09:22总费用:60€\ n抵达航班日期和时间28/03/2019 \ nAccommodation Name Divani Palace雅典卫城\ n住宿地址Parthenonos 19,Athina 117 42,希腊\ n评论\ nFlight出发日期29/03/2019 \ nAccommodation名称Divani Palace雅典卫城\ n住宿地址Parthenonos 19,Athina 117 42,Greece \ nComments
我想得到粗体的话。未加粗的单词是固定的。也就是说,除了粗体字之外,每张凭证都具有相同的格式。你可以看到有很多重复的单词,其中一些可能是两个甚至三个单词(例如经济舱或阿姆斯特丹酒店)。我现在正在做的是尝试在两个字符串之间获取文本。例如,如果我想获得经济汽车文本我会使用这个正则表达式:
text.match(/Details ([\s\S]*?) Maximum/)
但是这会返回None,我认为这是因为字符串中有很多值或者有重复的单词。我想避免for循环,因为我使用谷歌脚本,并有一个运行时限制。我该如何处理这个问题?
该文本看起来像是最初是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);
更新:更新代码以使用Apps脚本假设您需要一个脚本来解析多个类似的字符串。假设只有粗体文本发生变化。
基本算法将从结束开始并按字段解析。您需要一组字段名称:
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)
以上是关于在javascript(google脚本)中获取两个字符串之间的文本的主要内容,如果未能解决你的问题,请参考以下文章
Google 表格脚本中的 Javascript:帮助使用 setNumberFormat
在单独的JavaScript文件中运行Google跟踪代码管理器脚本?
使用javascript sdk将文件上传到Google云端硬盘
Google Chrome Javascript 调试器和内容脚本