Return matches of both the URL and the Link Text
Hi All,
How could i return matches of both the URL and the Link Text? if possible. If you know of a function that will do this, please point me to it.
My script returns works fine convertig URLs but i don't have the function to detect hyperlinks.
Any help will be greatly appreciated
Code:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
var breaks = [];
var replaceWordChars = function(text) {
var s = text;
// spaces
s = s.replace(/[\u02DC|\u00A0]/g, "");
return s;
}
function replaceURLWithHTMLLinks(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
return text.replace(exp,'<a href="$1">$1</a>');
}
$(document).ready(function(){
$("#input").bind("keypress keyup", function() {
breaks = $("#input").val().split(/[\r\n]+/g);
$("#storage").html("");
$.each(breaks,function(e){
$("#storage").append("<p>" + breaks[e] + "</p>\n");
});
$("#output").val(replaceURLWithHTMLLinks(replaceWordChars($("#storage").html().replace(/[\n]$/,""))));
});
});
</script>
<form name="subform">
<textarea id="input" name="oldText" cols="120" rows="30" wrap="virtual"></textarea>
<br />
<textarea id="output" name="newCode" cols="120" rows="30" wrap="virtual"></textarea>
<div id="storage" style="display:none"></div>
</form>