【问题标题】:FTL: How to retrieve "href" value from <a> linkFTL:如何从 <a> 链接中检索“href”值
【发布时间】:2016-12-12 12:28:47
【问题描述】:

我正在尝试从 Freemarker 中的链接中检索 href 值,如下所示:

<a href="http://www.example.com" class="some-class">Test</a>

【问题讨论】:

  • 那么,到目前为止,您尝试了什么?
  • @Kamiccolo 试过 ?matches 来选择 href。但没有运气
  • 可以使用Jsoup,用于解析html页面。
  • @SachinSarawgi 我正在寻找 Freemarker 特定的解决方案
  • @Depzor 能否请您发布您尝试选择 href 属性的代码。

标签: java jsp freemarker


【解决方案1】:

我尝试使用 Pattern 进行检索。你可以试试

String str = "<a href=\"http://www.example.com\" class=\"some-class\">Test</a>";
Pattern pattern = Pattern.compile("(href=\"(.*?)\")");
Matcher macther = pattern.matcher(str);
if(macther.find()){
    System.out.println(macther.group(1).substring(5));
}

【讨论】:

    【解决方案2】:

    您可以在 match 内置函数中对组使用正则表达式。

    例子:

    <#assign res='<a href="http://www.example.com">Test</a>'?matches(r'href="(.*?)"')>
    
    <#list res as m>
      ${m?groups[1]}
    </#list>
    

    这将产生:

    http://www.example.com
    

    【讨论】:

      猜你喜欢
      • 2012-12-11
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 2017-08-30
      • 2012-10-22
      • 2014-06-10
      • 2021-09-15
      • 2017-04-27
      相关资源
      最近更新 更多