如何在jQuery中以#标签开头的单词中添加类?
问题描述:
因此,假设我在jQuery中有一个看起来像这样的字符串
So let's say I have a string in jQuery that looks like
result.text = "I tweeted something #one #two #three";
有没有一种方法可以将所有以散列标签开头的单词包装在一个名为"tweet-hash"的跨度类中?
Is there a way to wrap all the words starting with a hashtag with a span class called "tweet-hash" ?
答
您可能想使用一些Regex查找符号/单词组合,并将其替换并用样式标签和CSS类包装.不幸的是,CSS没有这样的值选择器.
You'll probably want to use some Regex to find the symbol/word combination, and replace it and wrap it with a style tag and a CSS class. CSS unfortunately doesn't have a selector for values like that.
$("#phrase").html($("#phrase").html().replace(/#([^ ]+)/, "<span class='hashtag'>$1</span>")