Jquery 选择并在文本中换行字符串

Jquery 选择并在文本中换行字符串,jquery,html,twitter,Jquery,Html,Twitter,我有通过Twitter API生成的推文,例如: <article class="tweet"> Some users may have experienced issues accessing http://t.co/zDdcbPNfnU on @Firefox 22+. We've reached out to Mozilla and it's now resolved. </article> <article class="tweet"> T

我有通过Twitter API生成的推文,例如:

<article class="tweet">
    Some users may have experienced issues accessing http://t.co/zDdcbPNfnU on @Firefox 22+. We've reached out to Mozilla and it's now resolved.
</article>
<article class="tweet">
  The issue with viewing photos is now resolved. Thanks for your patience!
</article>

如何使用jQuery选择链接?

您可以尝试用RegExp替换它:

$('article.tweet').each(function(){
  var linkHref = $(this).html().replace(/http:\/\/\S+/g, function (match) {
     return "<a href='" + match + "'>" + match + "</a>"
  });
  $(this).html(linkHref);
});
$('article.tweet')。每个(函数(){
var linkHref=$(this.html().replace(/http:\/\/\S+/g,函数(匹配){
返回“”
});
$(this.html(linkHref);
});
replace()
的第二个参数中提供一个函数来格式化每个匹配的URL


你所做的是我会使用的方法,但我只是想知道你到底在包装什么。查找是否返回了任何结果?@DarrenCrabb的可能重复项查找没有返回任何内容,因为我认为它正在查找具有该名称的元素,而不是字符串搜索。部分问题是,
$.fn.html()
是一个方法,而不是一个成员,除非它只是一个键入错误。最后一定要和帕伦斯一起打电话。您是对的,
$.fn.find
实际上是一种基于选择器搜索子元素的方法,您传递它的内容甚至可能不会运行,因为最后的
://
将使它作为查询选择器无效。非常感谢!我以前没有使用过正则表达式,但这很好地完成了这项工作。@Coop不客气。您可以找到更好的正则表达式来匹配URL。
$('article.tweet').each(function(){
  var linkHref = $(this).html().replace(/http:\/\/\S+/g, function (match) {
     return "<a href='" + match + "'>" + match + "</a>"
  });
  $(this).html(linkHref);
});