正则表达式以相同的字符串开始和结束,而不仅仅是相同的字符

Regex start and end with same string, not just same character

我想创建一个正则表达式来接收:

<p class="MyClass">
   <p> something 1 </p>
   <p> something 2 </p>
   <span>         <span>  // or more html tag here
   something
</p>
something's here, not in any tag!

来自:

<p class="MyClass">
   <p> something 1 </p>
   <p> something 2 </p>
   <span>         <span>  // or more html tag here
   something
</p>
something's here, not in any tag!

<p class="MyClass">
   <p> another thing 1</p>
   <p> another thing 2</p>
   <p> another thing 3</p>
   another thing
</p>
...

我想我会使用正则表达式来匹配 <p class="MyClass"> 和下一个之间的所有内容。所以正则表达式是 /(<p class="MyClass">[\s\S]*)<p class="MyClass">/,在这种情况下可以正常工作。但是当我想得到这个页面的通知时它不起作用 http://daotao.dut.udn.vn/sv/G_Thongbao_LopHP.aspx。 DOM这么奇怪?!

抱歉我的英语不好。

正则表达式应该是

(<p class="MyClass">[\s\S]*?)(?=<p class="MyClass">|$)
  • [\s\S]*? : *? 是惰性量词,因此它匹配最短的默认值是贪婪的(匹配最大的)。
  • (?=<p class="MyClass">|$):lookhead 使其不属于匹配项,|$ 也得到最后一个匹配项