2010-06-08 33 views
0

任何人都有一個線索,我可以如何使用JQuery找到: http://或www。與.COM結尾,.ORG,埃杜 店,作爲一個變量,敷在jquery識別並製作超鏈接

<a href="variable"> <a/> 

所以我有一個textarea,當放鏈接的人,我想,然後使它們成爲一次超鏈接他們被張貼。

有意義嗎?

+1

如果你不想這樣做,直到它們被貼了,你想與服務器端的代碼,而不是要做到這一點jQuery的。如果您在提交之前使用JS創建帖子的預覽,那麼使用jquery會很有意義。 – intuited 2010-06-08 02:56:55

回答

1

可以說文本區域包含,

http://site1.com/uri/ 
http://site2.net/uri 
http://site3.org/uri 
http://site4.co.uk/uri 
http://site5.ws/uri 

我會做的第一件事就是綁定的即使對文字區域的鍵控也是如此

$('#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'); 
     } 
    }) 
}); 

沿着它們的線條!

正則表達式借用:http://snippets.dzone.com/posts/show/452

0

滾你自己的方式:

var foo = $('textarea selector').val(); 

然後使用正則表達式來得到你想要的。然後堅持它放回textarea的

$('textarea selector').val('http://thenewlink'); 

簡單的插件方式:

7 jQuery Plugins to Manipulate TEXTAREAs