Regex to wrap text with tags [duplicate]

爱⌒轻易说出口 提交于 2021-01-27 03:58:25

问题


I need a JavaScript regex to wrap "#hashtags" with <span> tags

Example:

Before: "I need #help, please. #Thanks"

After: "I need <span>#help</span>, please. <span>#Thanks</span>"

Currently, /#\w*\b/g finds all of the #hashtags but how do I wrap them in span tags?

Thanks!


回答1:


This works -

str = "I need #help, please. #Thanks"
str = str.replace(/(\#[a-zA-Z0-9\-\_]+)/g,"<span>$1</span>");
//> "I need <span>#help</span>, please. <span>#Thanks</span>"


来源:https://stackoverflow.com/questions/21651239/regex-to-wrap-text-with-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!