如何在某些文本中搜索任何和所有主题标签(字母数字和下划线和连字符)并将它们包装在跨度标签中,例如搜索
some_string = "this is some text with 3 hashtags #Tag1 and #tag-2 and #tag_3 in it"
并将其转换为:
"this is some text with 3 hashtags <span>#Tag1</span> and <span>#tag-2</span> and <span>#tag_3</span> in it"
到目前为止我有这个:
some_string = some_string.replace(/\(#([a-z0-9\-\_]*)/i,"<span>$1</span>");
但一个错误是它没有像它应该的那样在包装中包含#。它似乎输出:
"this is some text with 3 hashtags <span>Tag1</span> and #tag-2 and #tag_3 in it "
此外,它只检测到它遇到的第一个主题标签(例如#Tag1
在这个示例中),它应该检测到所有标签。
我还需要在 # 之后的主题标签至少包含 1 个字符。所以 # 本身不应该匹配。
谢谢