Simply, I want to parse URL link(s) and add html reference using jQuery, RegEx.
Source HTML is:
<div id="test"> my favorite search engines are http://www.google.com
and http://stackoverflow.com Do you agree? </div>
My Current Script is:
var exp = /(^|[^'">])((ftp|http|https):\/\/(\S+))(\b|$)/gi;
$('#test').html($('#test').html().replace(exp,
" <a href='$2' target='_blank'><img src='go.gif'/></a>"));
});
And its result is as following:
<div id="test"> my favorite search engines are
<a href="http://www.google.com" target='_blank'>
<img src='go.gif'/></a> and
<a href="http://stackoverflow.com" target='_blank'>
<img src='go.gif'/></a>
Do you agree? </div>
I know this script just replaces and can't preserve its source. I'd like to add its original URL right after 'img tag'. Anyone has an idea to get following result?
<div id="test"> my favorite search engines are
<a href="http://www.google.com" target='_blank'>
<img src='go.gif'/>http://www.google.com</a> and
<a href="http://stackoverflow.com" target='_blank'>
<img src='go.gif'/>http://stackoverflow.com</a>
Do you agree? </div>
Thank you for your comment in advance.