简体   繁体   中英

Regex to wrap text with tags

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!

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>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM