在JSTL中替换Anchor

时间:2013-06-30 05:10:05

标签: regex jsp servlets jstl

在我的JSP中,我收到了一些来自数据库的数据,我的数据就是这样的:

Google is the greatest search engine ever http://www.google.com

我想做的事情很简单:我想使用JSTL将这个链接包装在锚标签中:

Google is the greatest search engine ever <a href="http://www.google.com">http://www.google.com</a>

就是这样! 请注意网址不是常数,我的意思是我不确定这是什么&amp; amp;我刚刚在这里提到谷歌的例子。

1 个答案:

答案 0 :(得分:0)

按照this SO问题为JSTL创建replaceAll函数,然后使用以下模式将url替换为html链接:

        String pattern = "(http:[A-z0-9./~%]+)";

        String str = "Google is the http://www.test.com greatest search engine ever http://www.google.com";

        String replaced = str.replaceAll(pattern, "<a href='$1'>$1</a>");