javascript url to link

js:


<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>

html:


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 Comment