Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

[email protected]
<a href="mailto:[email protected]"> [email protected] </a>
like image 277
Mahmoud Saleh Avatar asked Dec 05 '25 10:12

Mahmoud Saleh


1 Answers

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

like image 50
codaddict Avatar answered Dec 07 '25 00:12

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!