Hi i need to detect and convert urls images and video from plain text urls
Html
<div id="content-url">
Hello World<br>
http://www.goalterest.com/ <br>
http://www.esotech.org/wp-content/uploads/2011/12/jquery_logo.png
http://www.esotech.org/wp-content/uploads
</div>
Jquery
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var photoRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]).(?:jpg|gif|png)/ig;
var url_url= $('#content-url').html().match(urlRegex);
var url_photo= $('#content-url').html().match(photoRegex);
var convert_url='<a href="'+url_url+'">'+url_url+'</a>';
var convert_photo='<img src="'+url_photo+'" width="150" height="150" alt="Nba">';
$('#content-url').append(convert_url);
$('#content-url').append(convert_photo);
Here Demo http://jsfiddle.net/nqVJc/3/
In the demo i get the way to detect and convert to url and photo but the problem ist when ist there multiple urls The Urls are not separated
You are not iterating through the matched elements :
$.each( url_url, function(i,value){
var convert_url='<a href="'+url_url[i]+'">'+url_url[i]+'</a>';
var convert_photo='<img src="'+url_photo[i]+'" width="150" height="150" alt="Nba">';
$('#content-url').append(convert_url,convert_photo)
});
Here is DEMO.
For removing matched urls you need to add this lines before $.each :
$('#content-url').html( $('#content-url').html().replace(urlRegex,''));
//$.each goes here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With