在 GWT 应用程序(使用 Java 7)中,我想为字符串变量中的每个 http/https 链接添加锚 html 标记。
它在嵌入模式下工作,但在生产中不工作。
private String clearTextContent(String txt) {
txt = txt.replaceAll("<", "<");
txt = txt.replaceAll(">", ">");
txt = txt.replaceAll("\n", "");
txt = txt.replaceAll("\\|", "<br/>");
// Display Link for URLs
txt = txt.replaceAll("(\\A|\\s)((http|https|ftp|mailto):\\S+)(\\s|\\z)", "$1<a target='_blank' href='$2'>$2</a>$4");
}
我也试过这个但不起作用:
String URLregexp = "(^|\\s)((http|https):\\S+)(\\s|$)";
txt = txt.replaceAll(URLregexp, "$1<a target='_blank' href='$2'>$2</a>$4");
我想要的例子:
输入:
Hello|http:///www.google.com|... That's it!
输出:
Hello|<a target='_blank' href='http:///www.google.com'>http:///www.google.com</a>|... That's it!
我读过在嵌入模式下 GWT 使用 javascript 正则表达式,而在生产中使用 java 正则表达式。
你有什么提示可以解决我的问题吗?
谢谢!