merveotesi merveotesi - 2 months ago 7
HTML Question

How can I linkify URLs which appear in plain text?

How can I linkify URLs which appear in plain text? For example,


Today i want to share a link: www.google.com


I would like to make "www.google.com" clickable by putting
<a>
tags around. Do I have to detect URLs and links with Java string parsing facilities and put
<a>
tags around them?

Answer

You could use the regexp from Matthew O'Riordan's page to match an URL with or even without the protocol part:

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/