This Regex ([a-zA-Z0-9]?(?:http[s]?://)?(?:(?:\w+)\.)(?:\S+)(?:\.(?:\w+))+?) successfully finds any occurances of http://www.msn.com or www.msn.com or msdn.microsoft.com or http://msdn.microsoft.com inside of a string. However, there are two replace algorithms that I need to use, this one <a href=\"$1\" target=\"_blank\" onclick=\"preventDualEditing(event)\">$1</a> and this one <a href=\"http://$1\" target=\"_blank\" onclick=\"preventDualEditing(event)\">$1</a>. Note the difference is that one prefixes the string with an http:// which is necessary for the links like www.msn.com.
But, what’s happening is the loop that I use the replace statements in is messing things up because if it’s trying to replace the www.msn.com the first occurance found is http://www.msn.com and so it replaces it multiple times – once when it finds the link with an http:// already and once when it find www.msn.com.
Is there a way for me to have a one pass replacement that would conditionally add the http:// to the string if necessary?
Try with this:
and use
$2instead of$1