简体   繁体   English

使用 Google Apps 脚本从字符串中提取特定的 URL

[英]Extract specific URL from a string with Google Apps Script

I have a string like:我有一个像这样的字符串:

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|

I need to extract the second URL:我需要提取第二个 URL:

https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.pdf

Each URL is contained between 2 sticks and changes with each script execution, but the extension is always the same (*.pdf).每个 URL 包含在 2 根棍子之间,每次脚本执行都会发生变化,但扩展名始终相同 (*.pdf)。

I believe your goal as follows.我相信你的目标如下。

  • 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. 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|`;

For this, how about this answer?为此,这个答案怎么样?

Pattern 1:模式一:

In this pattern, split is used.在此模式中,使用了split In this case, when the position of the URL you want is the same, this can be used.这种情况下,当你想要的URL的position是一样的时候,这个就可以用了。

Sample 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|`; const res = str.split("|")[3]; console.log(res)

Pattern 2:模式二:

In this pattern, regex is used.在此模式中,使用了正则表达式。 In this case, when the URL you want is enclosed by link_of_pdf|在这种情况下,当您想要的 URL 包含在link_of_pdf|中时and || , this can be used. , 这个可以用。

Sample 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|`; const res = str.match(/link_of_pdf\|(.+)\|/)[1]; console.log(res)

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM