Detect emails in a text and surrond it with the <a> tag

谁说胖子不能爱 提交于 2019-12-10 19:10:45

问题


greetings all I have a text that may contains emails and I want to detect any email occurrence and surround it with the < a > tag ex:

my.email@mycompany.com
<a href="mailto:my.email@mycompany.com"> my.email@mycompany.com </a>

回答1:


Using the regex from regular-expression.info you can do:

text = text.replaceAll("(?i)\\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4})\\b",
                       "<a href=\"mailto:$1\"> $1 </a>");            

Ideone Link



来源:https://stackoverflow.com/questions/4561464/detect-emails-in-a-text-and-surrond-it-with-the-a-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!