标记内容的正则表达式

时间:2011-03-25 09:59:58

标签: javascript regex strip-tags

我使用 Javascript 我有这个:

<(div|span) class="search-result-(body-text|title)">(.*?)</(span|div)>

我使用的是这个内容:

<div class="search-result-item club">
   <span class="search-result-type">Projekt</span
   <span class="search-result-title">Titel</span>
   <div class="search-result-body-text">
     Body text
   </div>
   <div class="search-result-attributes">
     <span class="search-result-attribute">Attribute</span>
   </div>
 </div>

我的结果是:

<span class="search-result-title">Titel</span>,
<div class="search-result-body-text">
  Body text
</div>

多数民众赞成有道理,但我的正则表达式应该怎样才能剥离标签,所以我只能得到: Titel 正文

1 个答案:

答案 0 :(得分:4)

法律要求有人发布此链接:RegEx match open tags except XHTML self-contained tags您应该阅读并重新考虑您是否想要使用正则表达式解析HTML。

但是,你想要的是匹配中第三个()组的内容。 JS正则表达式对象的exec方法是一个数组,其中包含索引0处的整个匹配,以及来自索引1,2,...的所有组的匹配(在这种情况下,索引3是您需要的)。

[注意:此答案的早期版本上面有“第一个”和“1”而不是“第三个”和“3”,因为我误读了你的正则表达式。遗憾。]

相关问题