r1se
r1se

Reputation: 67

regexp for hashtag/mention in href

My goal make html hastag, for this i'm need wrap text with # into

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

I wan't make regexp which can give me words with # and @, but i'm have some trouble with URLs like this: http://gitlab.com/#xxx or https://medium.com/@erikdkennedy

My example string:

<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>

My regexp is:

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

I get 2 matches #test and last #xxx (https://regex101.com/r/pXxIkf/1)

How i can get only test, and dont find inside the href definition?

Thank you!

Upvotes: 0

Views: 67

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

Try this :

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

(?<=>) Positive Lookbehind to make sure that there is > before the hashtag.

(?: start non-capturig group.

[\s]* there is whitespace or not.

(?:#|@) non-capturig group that make sure there either # or @

DEMO

Upvotes: 1

Related Questions