Javascript 如何识别和制作超链接?

Javascript 如何识别和制作超链接?,javascript,jquery,hyperlink,Javascript,Jquery,Hyperlink,如何使用Jquery查找http://或www.,并以.com,.org,.edu结尾,将其存储为变量并将其包装为: <a href="variable"> <a/> 我有一个文本区,当人们输入链接时,我想在他们发布后将其制作成超链接。 按自己的方式滚动: var foo = $('textarea selector').val(); 然后使用正则表达式得到所需的。然后将其粘贴回文本区域 $('textarea selector').val('http://th

如何使用Jquery查找
http://
www.
,并以
.com
.org
.edu
结尾,将其存储为变量并将其包装为:

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

我有一个文本区,当人们输入链接时,我想在他们发布后将其制作成超链接。


按自己的方式滚动:

var foo = $('textarea selector').val();
然后使用正则表达式得到所需的。然后将其粘贴回文本区域

$('textarea selector').val('http://thenewlink');
$('#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://site1.com/uri/
http://site2.net/uri
http://site3.org/uri
http://site4.co.uk/uri
http://site5.ws/uri
我要做的第一件事是在textarea上绑定一个偶数键

$('textarea selector').val('http://thenewlink');
$('#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');
       }
    })
});
$('addLinks').bind('keyup',function(){
var_data=$(this.val();
//然后我会尝试分割空间和车厢
_array=_data.split('\n |\r\n |\s |,');//按返回、新行、空格和逗号分割。
var\u regexp=/(ftp | http | https):\/\(\w+:{0,1}\w*@)(\S+:[0-9]+)(\/\/([\w\!:?+=&%@!\-\/])/
$(_数组).each(函数(值){
//现在我将检查URL是否有效
如果(_正则表达式测试(值))
{
//有效的URL,因此我将随后将其附加到链接容器中
//在这里你可以做e

$('

如果您不想在发布帖子之前执行此操作,则需要使用服务器端代码而不是jquery。如果您在提交帖子之前使用JS创建帖子预览,则使用jquery是有意义的。