Java : replacing text URL with clickable HTML link

前端 未结 6 765
不知归路
不知归路 2020-12-20 13:54

I am trying to do some stuff with replacing String containing some URL to a browser compatible linked URL.

My initial String looks like this :

\"hell         


        
相关标签:
6条回答
  • 2020-12-20 14:03

    Try to use:

    myString.replaceAll("(.*://[^<>[:space:]]+[[:alnum:]/])", "<a href=\"$1\">HereWasAnURL</a>");
    

    I didn't check your regex.

    By using () you can create groups. The $1 indicates the group index. $1 will replace the url.

    I asked a simalir question: my question
    Some exemples: Capturing Text in a Group in a regular expression

    0 讨论(0)
  • 2020-12-20 14:06

    In case of multiline text you can use this:

    text.replaceAll("(\\s|\\^|\\A)((http|https|ftp|mailto):\\S+)(\\s|\\$|\\z)",
            "$1<a href='$2'>$2</a>$4");
    

    And here is full example of my code where I need to show user's posts with urls in it:

    private static final Pattern urlPattern = Pattern.compile(
            "(\\s|\\^|\\A)((http|https|ftp|mailto):\\S+)(\\s|\\$|\\z)");
    
    
    String userText = ""; // user content from db
    String replacedValue = HtmlUtils.htmlEscape(userText);
    replacedValue = urlPattern.matcher(replacedValue).replaceAll("$1<a href=\"$2\">$2</a>$4");
    replacedValue = StringUtils.replace(replacedValue, "\n", "<br>");
    System.out.println(replacedValue);
    
    0 讨论(0)
  • 2020-12-20 14:07

    Assuming your regex works to capture the correct info, you can use backreferences in your substitution. See the Java regexp tutorial.

    In that case, you'd do

    myString.replaceAll(....., "<a href=\"\1\">\1</a>")
    
    0 讨论(0)
  • 2020-12-20 14:13

    For anybody who is searching a more robust solution I can suggest the Twitter Text Libraries.

    Replacing the URLs with this library works like this:

    new Autolink().autolink(plainText) 
    
    0 讨论(0)
  • 2020-12-20 14:21
    public static String textToHtmlConvertingURLsToLinks(String text) {
        if (text == null) {
            return text;
        }
    
        String escapedText = HtmlUtils.htmlEscape(text);
    
        return escapedText.replaceAll("(\\A|\\s)((http|https|ftp|mailto):\\S+)(\\s|\\z)",
            "$1<a href=\"$2\">$2</a>$4");
    }
    

    There may be better REGEXs out there, but this does the trick as long as there is white space after the end of the URL or the URL is at the end of the text. This particular implementation also uses org.springframework.web.util.HtmlUtils to escape any other HTML that may have been entered.

    0 讨论(0)
  • 2020-12-20 14:22

    Belows code replaces links starting with "http" or "https", links starting just with "www." and finally replaces also email links.

      Pattern httpLinkPattern = Pattern.compile("(http[s]?)://(www\\.)?([\\S&&[^.@]]+)(\\.[\\S&&[^@]]+)");
    
      Pattern wwwLinkPattern = Pattern.compile("(?<!http[s]?://)(www\\.+)([\\S&&[^.@]]+)(\\.[\\S&&[^@]]+)");
    
      Pattern mailAddressPattern = Pattern.compile("[\\S&&[^@]]+@([\\S&&[^.@]]+)(\\.[\\S&&[^@]]+)");
    
        String textWithHttpLinksEnabled = 
      "ajdhkas www.dasda.pl/asdsad?asd=sd www.absda.pl maiandrze@asdsa.pl klajdld http://dsds.pl httpsda http://www.onet.pl https://www.onsdas.plad/dasda";
    
        if (Objects.nonNull(textWithHttpLinksEnabled)) {
    
          Matcher httpLinksMatcher = httpLinkPattern.matcher(textWithHttpLinksEnabled);
          textWithHttpLinksEnabled = httpLinksMatcher.replaceAll("<a href=\"$0\" target=\"_blank\">$0</a>");
    
          final Matcher wwwLinksMatcher = wwwLinkPattern.matcher(textWithHttpLinksEnabled);
          textWithHttpLinksEnabled = wwwLinksMatcher.replaceAll("<a href=\"http://$0\" target=\"_blank\">$0</a>");
    
          final Matcher mailLinksMatcher = mailAddressPattern.matcher(textWithHttpLinksEnabled);
          textWithHttpLinksEnabled = mailLinksMatcher.replaceAll("<a href=\"mailto:$0\">$0</a>");
    
          System.out.println(textWithHttpLinksEnabled);
        }
    

    Prints:

    ajdhkas <a href="http://www.dasda.pl/asdsad?asd=sd" target="_blank">www.dasda.pl/asdsad?asd=sd</a> <a href="http://www.absda.pl" target="_blank">www.absda.pl</a> <a href="mailto:maiandrze@asdsa.pl">maiandrze@asdsa.pl</a> klajdld <a href="http://dsds.pl" target="_blank">http://dsds.pl</a> httpsda <a href="http://www.onet.pl" target="_blank">http://www.onet.pl</a> <a href="https://www.onsdas.plad/dasda" target="_blank">https://www.onsdas.plad/dasda</a>
    
    0 讨论(0)
提交回复
热议问题