用于匹配主题标签的正则表达式模式,但不在 HTML 属性中

Regex pattern to match hashtag, but not in HTML attributes

我正在尝试 使用正则表达式 #([a-z0-9_]+) 在 HTML 文本 中提取主题标签,但在 HTML 属性中遇到问题.

例如在 HTML 文本中:

hola que tal with #hash1.
hola que tal with #hash2

y <a href="hola.que.tal#hash3"> para #hash4. </a>

我想恢复 "hash1"、"hash2" 和 "hash4" 但 不想 "hash3".

我尝试通过环视来解决它,表达式如下:

(?<!<)#([a-z0-9_]+)(?!.*?>)

但没有成功。

如何使用 单个正则表达式

这应该有效

/#[a-z0-9_]+(?![^<]*>)/

http://www.regexpal.com/?fam=95144

负前瞻的作用是确保在主题标签和下一个 > 之间有一个 <