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>");
You can just use this in the regex:
/(^|\s)([#@][a-z\d-]+)/
To match both @ and #.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With