简体   繁体   English

JavaScript惰性正则表达式,用于匹配HTML标签

[英]JavaScript lazy regex for matching HTML tags

I'm having a problem writing a regular expression for matching HTML tags. 我在编写用于匹配HTML标签的正则表达式时遇到问题。 I found a similar entry here , but this didn't quite work in my case. 我在这里找到了类似的条目 ,但是对于我来说,这并不起作用。

Here's my test string: 这是我的测试字符串:

<div id="div0" class="myclass">here's some text
that may include whitespace</div><div id="div1" class="myclass">
and some more here
</div>

And here's my regex based on the aforementioned entry: 这是基于上述条目的正则表达式:

<div[^>]*class="myclass">[^~]*?<\/div>

Note that I need to match the first instance of <div /> with class of "myclass." 请注意,我需要将<div />的第一个实例与“ myclass”类进行匹配。 The content may have carriage returns. 内容可能有回车符。 These <div> tags won't be nested. 这些<div>标签不会嵌套。

Here's a rubular page for testing: http://rubular.com/r/vlfcikKMXk 这是用于测试的表格页面: http ://rubular.com/r/vlfcikKMXk

That regex tested is not great. 经测试的正则表达式不是很好。 It is in fact matching as you want it to, but it is matching it multiple times (2 different matches), and not showing a difference, you only want the first match. 实际上,它可以按您希望的方式进行匹配,但是它可以多次匹配(2个不同的匹配项),并且没有显示差异,您只需要第一个匹配项。

Go here: http://gskinner.com/RegExr/ 前往这里: http//gskinner.com/RegExr/

Test it there, turn off the 'global' you will see it working. 在此处进行测试,关闭“全局”,您将看到它正在运行。

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

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