[英]In Javascript Regex, how do I match hash tags, but not with the HTML code?
This is how I get the tags of a body of text. 这就是我获取正文标记的方式。
var tags = body.match(/#([a-z0-9]+)/gi);
However, if the sentence is: 但是,如果句子是:
The brown #fox jumped over ‘ fence.
The regex above will treat "8216;" 上面的正则表达式将处理“ 8216”; as a tag, which is what I do not want. 作为标签,这是我不想要的。 I only want "fox" as a tag. 我只想要“ fox”作为标签。
Note: I just want a basic regex solution. 注意:我只想要一个基本的正则表达式解决方案。
Try this one: 试试这个:
/(^#|\s#)([a-z0-9]+)/gi
LIVE DEMO: http://jsfiddle.net/DerekL/NpjyR/ 实时演示: http : //jsfiddle.net/DerekL/NpjyR/
or this: 或这个:
/(^#|[^&]#)([a-z0-9]+)/gi //this will exclude every &#
Assuming you have access to the DOM, you could use the DOM to decode the HTML and then match on the text content: 假设您有权访问DOM,则可以使用DOM解码HTML,然后匹配文本内容:
var temp = document.createElement('div');
temp.innerHTML = body;
var tags = temp.textContent.match(/#([a-z0-9]+)/gi);
试试这个:
#([a-z0-9]+)\b(?!;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.