[英]Extract specific URL from a string with Google Apps Script
我有一个像这样的字符串:
link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755|
link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf|
link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|
我需要提取第二个 URL:
https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf
每个 URL 包含在 2 根棍子之间,每次脚本执行都会发生变化,但扩展名始终相同 (*.pdf)。
我相信你的目标如下。
You want to retrieve https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf
from the following string using Google Apps Script.
const str = `link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755| link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf| link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|`;
为此,这个答案怎么样?
在此模式中,使用了split
。 这种情况下,当你想要的URL的position是一样的时候,这个就可以用了。
const str = `link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755| link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf| link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|`; const res = str.split("|")[3]; console.log(res)
在此模式中,使用了正则表达式。 在这种情况下,当您想要的 URL 包含在link_of_pdf|
中时和|
, 这个可以用。
const str = `link|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755| link_of_pdf|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf| link_of_xml|https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.xml|`; const res = str.match(/link_of_pdf\|(.+)\|/)[1]; console.log(res)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.