javascript url to link

<script>
function url_to_link(text) {
    var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
    return text.replace(exp,"<a href='$1'>$1</a>");
}
 
$(function(){
    var text = $('.target').html();
    $('.result').html( url_to_link(text) );
});</script>
src:
<div class="well target">
    google: https://www.google.com/
    google secured: http://www.google.com/
</div>
 
result:
<div class="well result"></div>

Leave a Reply

Your email address will not be published. Required fields are marked *