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
Recommended Posts