java处理文本中的httphttps链接

xiaoxiao2021-02-27  156

private String changeUrl(String content){ String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(content); StringBuffer result = new StringBuffer(); while (matcher.find()) { String urlStr=matcher.group(); StringBuffer replace = new StringBuffer(); replace.append("<a href=\""+urlStr+"\""); replace.append(" onClick=\""+"window.open('"+urlStr+"')\""+">"+urlStr+"</a>"); matcher.appendReplacement(result, replace.toString()); } matcher.appendTail(result); System.out.println(result); return result.toString(); }
转载请注明原文地址: https://www.6miu.com/read-13998.html

最新回复(0)