Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detecting hashtags and @ in string

I am trying to detect hashtags and @ and add the class to the world so as I can add a styling. Right now it is working but I am iteration over text twice. Can I do it with a one statement?

var text = item["text"].replace(/(^|\s)(#[a-z\d-]+)/ig, "$1<span class='hash_tag'>$2</span>");
    text = text.replace(/(^|\s)(@[a-z\d-]+)/ig, "$1<span class='hash_tag'>$2</span>");
like image 266
lipenco Avatar asked Nov 26 '25 20:11

lipenco


1 Answers

You can just use this in the regex:

/(^|\s)([#@][a-z\d-]+)/

To match both @ and #.

like image 104
MightyPork Avatar answered Nov 28 '25 14:11

MightyPork