【问题标题】:Extract specific URL from a string with Google Apps Script使用 Google Apps 脚本从字符串中提取特定 URL
【发布时间】:2020-06-01 16:59:34
【问题描述】:

我有一个类似的字符串:

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|

我需要提取第二个网址:

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

每个 URL 都包含在 2 个棒之间,并且每次脚本执行都会发生变化,但扩展名始终相同 (* .pdf)。

【问题讨论】:

    标签: javascript string url google-apps-script


    【解决方案1】:

    我相信你的目标如下。

    • 您想使用 Google Apps 脚本从以下字符串中检索 https://www.nubefact.com/cpe/3a5c76ea-9447-4f34-9b90-7f514345cbf8-474b0936-c936-49f5-acb6-9e94102ce755.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|`;
      

    对于这个,这个答案怎么样?

    模式一:

    在此模式中,使用split。在这种情况下,当你想要的 URL 的位置相同时,可以使用这个。

    示例脚本:

    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)

    模式 2:

    在此模式中,使用了正则表达式。在这种情况下,当你想要的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)

    参考资料:

    【讨论】:

    • 完美,我一直在使用与您的第一个解决方案类似的代码,但即使我更改了 URL 的位置,第二个也可以向我保证相同的结果。谢谢。
    • @J. Kenneth Reátegui B. 感谢您的回复和测试。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2020-05-09
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    相关资源
    最近更新 更多