简体   繁体   English

Javascript Regex:获取HTML标签

[英]Javascript Regex: Get HTML Tags

I made this regex: 我做了这个正则表达式:

/\<+[a-zA-Z0-9\=\"\s]+\>+.+\<\/+[a-zA-Z0-9]+\>/gi

which matches a full html tag like: 它匹配一个完整的HTML标签,如:

<p>this is a paragraph</p>

But the problem with this that that it matches all of the elements as one match 但是这个问题是它将所有元素匹配为一个匹配

<div><p>this is a paragraph</p></div>

But I would like to get all of the HTML elements separated. 但我想将所有HTML元素分开。

Note: The HTML tags are in a string not in the DOM. 注意:HTML标记位于不在DOM中的字符串中。

Before the regex solution I tried to create a new div element and I added the string as it's innerHTML. 在正则表达式解决方案之前,我尝试创建一个新的div元素,并添加了字符串,因为它是innerHTML。 But doesn't worked properly I don't really know why... 但是不能正常工作我真的不知道为什么......

So I'm looking for a REGEX solution which solves this one match problem. 所以我正在寻找解决这一匹配问题的REGEX解决方案。

Thanks 谢谢

Replacing the inner +.+ with +[^<]+ would prevent it from matching the whole string, but regular expressions are not the correct choice for processing strings that contain nested components. +[^<]+替换内部+.+会阻止它匹配整个字符串,但正则表达式不是处理包含嵌套组件的字符串的正确选择。 For that you should be using a parser. 为此你应该使用解析器。

Regular expressions are simply the wrong tool for the job here. 正则表达式只是这里工作的错误工具。

Regular expressions are not appropriate to handle html. 正则表达式不适合处理html。 As you mention that the HTML is not part of the DOM 正如您所提到的,HTML不是DOM的一部分

Note: The HTML tags are in a string not in the DOM. 注意:HTML标记位于不在DOM中的字符串中。

You can use JQuery to build an object from the HTML and use DOM selectors / traversion to work with it: 您可以使用JQuery从HTML构建对象,并使用DOM选择器/ traversion来处理它:

$(myHTMLString).find('p')...

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

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