简体   繁体   English

识别JavaScript中的#标签

[英]Recognising Hashtags in Javascript

I need to recognise hashtags (#example) in a string using javascript and change them to 我需要使用javascript识别字符串中的主题标签(#example)并将其更改为

<a href='#/tags/example'>example</a>

at the moment I am using this code: 目前,我正在使用此代码:

var string = '#hello This is an #example of some text';

string.replace(/(^|\s)(#[a-z\d-]+)/ig, "$1<a href='#/tags/$2'>$2</a>");

however the value of $2 still includes the # character and I need to omit this for the url 但是$ 2的值仍包含#字符,我需要在url中省略它

您可以将#移动到捕获组之外:

/(^|\\s)#([az\\d-]+)/ig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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