[JAVA] Enclose the URL in TXT received by JSP with an automatic link tag

At the beginning

Find the URL from the string and automatically generate the tag.

Text Area and String pulled from the database There were many reference sites for enclosing only URLs with link tags, It is that the URL link is identified from the character string that is mixed with the character string that is not the URL It wasn't easy so I made a note


Specifically, use the replaceAll method. Don't forget to declare

<%@ page import="java.util.regex.*" %>

Suppose that a variable called Mozi contains the character string you want to automatically generate.

String result = Mozi.replaceAll("(http://|https://){1}[\\w\\.\\-/:]+","<a href='$0'>$0</a>");

This will create a text with a link tag that is automatically generated in result. The rest is free to boil or bake

Regular expressions are important


Reference site (http://www.atmarkit.co.jp/ait/articles/0406/01/news101.html)

Recommended Posts

Enclose the URL in TXT received by JSP with an automatic link tag