问题
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