我正在尝试解析原始推文字符串以匹配@username和#topic部分,我能够匹配它们,但我不熟悉如何包装它们。
我的代码:
"Hey @someotheruser this is a tweet about #topic1 and #topic1".replace(/(^|)@(\w+)/, '<span class="mention">?result of match?</span>');
"Hey @someotheruser this is a tweet about #topic1 and #topic1".replace(/(^|)#(\w+)/, '<span class="hash">?result of match?</span>');
所以我的问题是:如何获得比赛的结果,并用跨度包裹它?
最佳答案
var my_string = 'Hey @someotheruser this is a tweet about #topic1 and #topic1';
my_string = my_string.replace(/(\@\w+)/g, '<span>$&</span>');
my_string = my_string.replace(/(\#\w+)/g, '<span>$&</span>');
console.log(my_string);
输出
Hey <span>@someotheruser</span> this is a tweet about <span>#topic1</span> and <span>#topic1</span>
关于javascript - 匹配@username和#topic并将结果包装在span中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15901783/