English 中文(简体)
如何在使用jquery的案文中找到联系?
原标题:How to find link in a text using jquery?

Lets say I have a text such as this "This is a long text. It contains 150 characters. You can find more about this text on this link http://www.somewebsite.com/RDFCCSDVDS".

So in above text I want to find that link and convert it into a link so that when user clicks on it, the user will be taken directly to this website.

我如何能够实现这一目标?

最佳回答

我怀疑,我可以在这方面作许多改进,尽管此时此刻,这是我所能提供的最佳办法(尽管我认为某些替代办法可能更有效地发挥作用):

var $words = $( p ).text().split(   );
for (i in $words) {
    if ($words[i].indexOf( http:// ) == 0) {
        $words[i] =  <a href="  + $words[i] +  ">  + $words[i] +  </a> ;
    }
}

$( p ).html($words.join(   ));

JS Fiddle demo.

略微改进了上述版本(但好的住所,令人费解):

var punctuation = [ ! ," ", " , , , . ];
$( p ).each(
    function(){
        $words = $(this).text().split(   );
        for (i in $words){
            if ($.inArray($words[i].charAt(0),punctuation) > -1 && $words[i].indexOf( http:// ) == 1){
                alert($words[i]);
            }
            else if ($.inArray($words[i].charAt($words[i].length - 1),punctuation) > -1 && ($words[i].indexOf( http:// ) == 1 || $words[i].indexOf( http:// ) == 0)){
                $words[i] =  <a href=" +$words[i].substring(0,$words[i].length-1)+ ">  + $words[i].substring(0,$words[i].length-1) +  </a>  + $words[i].charAt($words[i].length-1);
            }
            else if ($words[i].indexOf( http:// ) == 0){
                $words[i] =  <a href="  + $words[i] +  ">  + $words[i] +  </a> ;
            }
        }
        $(this).html($words.join(   ));
    });

JS Fiddle demo

我不敢肯定在引用的链接方面可以做些什么,但(例如<编码>”http://google.com>;而且,坦率地说,我认为,这方面的办法可能很远,far

问题回答

定期表达:

$( p ).html(function(i, text) {
     return text.replace(
         /http://([w.-]+.)+[a-z]{2,}/.+/gi,
          <a href="$&">$&</a> 
     );
});

demo

@Eric 解决办法是精彩的,但我发现,如果尿后有空位,那会是错误的。 以类似案文为实例打他的 j:

<p>This is a long text. It contains 150 characters. You can find more about this text on this link http://api.som-ewebsite.com/RDFCCS-VDS/#!/?p1=foo&p2=bar right here</p>​​​​​​​​​​​​​​​​​​​​​​​

因此,我改变了格列斯的最近部分,以拦截除此面积外的每一种无害的URL型特性,而后者是我的一个“决定者”(在现实世界中,它当然不是,但我找不到一种办法,将属于URL的正常空间和空间分开。

$( p ).html(function(i, text) {
    return text.replace(
        /http://([w.-]+.)+[a-z]{2,}([/w.-\_?=!#,&amp;]+)/gi,
         <a href="$&">$&</a> 
    );
});​​

我认为,这可以改进,欢迎建议总是:

EDITED:

无论如何,我想这是围绕以下几个方面的最佳解决办法:。 如何用链接取代平原URLs?

定期表达

var e = /http://([A-Za-z0-9.-_]{2,}.)+[A-Za-z]{2,}(/.+)/,
    s = "This is a long text. It contains 150 characters. You can find more about this text on this link http://www.somewebsite.com/RDFCCSDVDS";

if (s.match(e)) {
    var url = RegExp[ $& ];
    s = s.replace(url,  <a href="  + url +  ">  + url +  </a> );
}

在座标中,您可使用主角的标签接上电页链接。

因此,如果你想与网页上的每一环节做些什么,你就可以使用。

$("a").each(function() {
    //your code here. you can access the link by using $(this)
});




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签