I currently have the following regex to capture link text and a URL in the following format:
[Link](http://link.com)
\[(.+)]\(((https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}))\)
When I add another expression afterwards to linkify URLs, it messes up ones in the above format.
Is there a singular regular expression to handle both cases?
http://link.com
-> <a href="http://link.com" target="_blank">http://link.com</a>
[Link](http://link.com)
-> <a href="http://link.com" target="_blank">Link</a>
PHP:
$string = preg_replace('/\[(.+)]\(((https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}))\)/', '<a href="$2" target="_blank">$1</a>', $string);