I am trying to write a function to create tags out of regular links and tags out of image links from the text in a text area.
it works the first time for both, but if i paste an "a href" tag in there, it double links it. it doesn't do the images because of the imageRegex check. any ideas how to get this to work?
keep in mind the textarea could have multiple urls of both types.
$("#message").blur(function() {
var imageRegex = /\.(png|jpg|jpeg|gif)$/;
var s = $(this).val().replace(/(?:^|[^"'])(\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|])/gim, function(str) {
if (str.match(imageRegex)) {
return('<img src="' + str + '" />');
} else {
return('<a href="' + str + '">' + str + '</a>');
}
});
$(this).val(s);
});