正则表达式中的标签/提示在href中

问题描述 投票:0回答:1

我的目标是制作html主题标签,为此我需要用#into包装文本

<a class="tag"><span class="hash">#</span>text</a>

我不会制作regexp,可以用#和@给我一些文字,但是我对这样的网址有些麻烦:http://gitlab.com/#xxxhttps://medium.com/@erikdkennedy

我的示例字符串:

<p>Some text <span class="highlighted">#test</span><br />
<a href="http://gitlab.com/#xxx" target="_blank" class="link">gitlab.com/#xxx</a><br />
<code>some feature</code></p>

我的正则表达式是:

(?!.*(<mail-link|link))#([a-zA-Z0-9]+)

我得到2场比赛#test和最后#xxx(https://regex101.com/r/pXxIkf/1

我怎么才能得到测试,并且在href定义中找不到?

谢谢!

regex formatting hashtag mention
1个回答
1
投票

试试这个 :

(?<=\>)(?:[\s]*(?:#|@))([a-zA-Z0-9]+)

(?<=>)正面Lookbehind确保在hashtag之前有>

(?:启动非捕获组。

[\ s] *是否有空格。

(?:#| @)非捕获组,确保有qazxsw poi

# or @

© www.soinside.com 2019 - 2024. All rights reserved.