检测字符串中的hashtags和@ [英] detecting hashtags and @ in string

查看:59
本文介绍了检测字符串中的hashtags和@的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测主题标签和@并将该类添加到世界中,以便我可以添加样式。
现在它正在工作,但我迭代文本两次。
我可以用一个声明吗?

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-]+)/

匹配@和#。

这篇关于检测字符串中的hashtags和@的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆