Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to wrap text with tags [duplicate]

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!

like image 926
Shu Avatar asked Feb 08 '14 20:02

Shu


1 Answers

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>"
like image 175
Kamehameha Avatar answered Oct 13 '22 12:10

Kamehameha