admin管理员组文章数量:1404570
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.
Here is the replace()
code:
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.
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.
Could somebody help me do that please?
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.
Here is the replace()
code:
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.
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.
Could somebody help me do that please?
Share Improve this question edited Jul 22, 2015 at 5:16 Tushar 87.3k21 gold badges163 silver badges181 bronze badges asked Jul 22, 2015 at 4:17 user3776241user3776241 5439 silver badges21 bronze badges 2- see the doc for each – Bhojendra Rauniyar Commented Jul 22, 2015 at 4:19
- You want to change value of text box or content of the div? – Mitul Commented Jul 22, 2015 at 4:31
2 Answers
Reset to default 5$('.your-tweet-class').each(function() {
$(this).text( replace( $(this).text() ) );
});
You can remove the spaces, I just put them in for clarity.
You can use html()
as follow.
Set the HTML contents of each element in the set of matched elements.
$('.tweetClass').html(function(index, oldHtml) {
return replace(oldHtml);
});
本文标签: javascriptHow do I use jQuery39s each() to replace textregexStack Overflow
版权声明:本文标题:javascript - How do I use jQuery's .each() to replace textregex? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744841521a2627949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论