在GWT中检测复杂的URL

时间:2014-12-03 10:28:12

标签: regex gwt

我一直在搜索用于检测文本中的网址的正则表达式,以便我可以用超链接替换它们。我对www.facebook.comhttps://google.com这样的普通网址实现了同样的效果,但在https://xyzSite.com/getUser.action?id=123&pwd=5等某些情况下失败了。您如何建议我可以使用上述参数检测网址。

我目前正在使用以下正则表达式:

(((ftp|http|https)://)?[\\w@.\\-\\_]+\\.[a-zA-Z]{2,}(:\\d{1,5})?(/[\\w#!:.?+=&%@!\\_\\-/]+)*)

1 个答案:

答案 0 :(得分:0)

我发现以下功能解决了我的问题:

public static String insertAnchorsForURLs(String ans) {
        return ans.replaceAll("((https?|ftp|file)://)?[\\w@.\\-\\_]+\\.[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]", "<a target='_blank' href = \'$0\'>$0</a>");
    }

我已经对此进行了以下测试:

www.google.com
www.somesite.org.in
www.mysite.com/whatever.action
www.mysite.com/whatever.action?id=1&pwd=2
https://google.com
http://google.co.in
相关问题