How to recognize and make hyper links?
How I can use Jquery to find http://
or www.
开发者_高级运维and ends with .com
, .org
, .edu
store that as a variable and wrap it in like:
<a href="variable"> <a/>
I have a textarea and when people put in links, I would like to then make them into hyperlinks once they are posted.
lets say the text area contains,
http://site1.com/uri/
http://site2.net/uri
http://site3.org/uri
http://site4.co.uk/uri
http://site5.ws/uri
The first thing i would do is bind an even for keyup on the textarea
$('#addLinks').bind('keyup',function(){
var _data = $(this).val();
//Then i would try and split the spaces and carriages
_array = _data.split('\n|\r\n|\s|,'); //Split by returns,new lines,spaces,commas.
var _regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
$(_array).each(function(value){
//Now i will check the URL is valid
if(_regex.test(value))
{
//Valid URL so i will then append it to the links container
//Here you can do e
$('<a></a>').attr('href',value).val(value).appendTo('.linksContainer');
}
})
});
Something along them lines!
Regex borrowed from: http://snippets.dzone.com/posts/show/452
roll your own way:
var foo = $('textarea selector').val();
then use a regular expression to get what you want. then stick it back into the textarea
$('textarea selector').val('http://thenewlink');
easy plugin way:
7 jQuery Plugins to Manipulate TEXTAREAs
精彩评论