[英]How do I use jQuery's .each() to replace text / regex?
I have this code right here: 我在这里有此代码:
var tweetText = $("#tweet_text").text();
var replaced = replace(tweetText);
document.getElementById("tweet_text").innerHTML = replaced;
It gets text from a text element, and highlights the mentions and URLS to make them clickable. 它从文本元素中获取文本,并突出显示提及和URL,以使它们可单击。 Here is the replace()
code: 这是replace()
代码:
function replace(text) {
return text.replace(/([@#])([a-z\d_]+)/ig, function(_, marker, tag) {
if (marker === "@")
return '<a href="?r=site/twitterprofile&q=$1">' + "@" + tag + '</a>';
return '<a href="?r=site/hashtag&q=$2">' + "#" + tag + '</a>';
});
}
I have a long list of divs / tweets, and when I run replace()
it only applies the pattern matching function to one div. 我的div / tweets列表很长,当我运行replace()
它仅将模式匹配功能应用于一个div。 To get around this, I thought of collecting all of the #tweet_text
divs using .each()
, and then applying the replace()
function once it loops through. 为了解决这个问题,我想到了使用.each()
收集所有#tweet_text
div的方法,然后在循环通过后应用replace()
函数。
Could somebody help me do that please? 有人可以帮我吗?
$('.your-tweet-class').each(function() {
$(this).text( replace( $(this).text() ) );
});
You can remove the spaces, I just put them in for clarity. 您可以删除空格,为清楚起见,我仅将其放入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.