开发者

Regexp for content inside tags

I use Javascript I have this:

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

And i use is on this开发者_JS百科 content:

<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>

My result is:

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

Thats make sense, but how should my regexp look like so it strips the tags, so i only get: Titel, Body text


It is required by law that someone post a link to this: RegEx match open tags except XHTML self-contained tags which you should read and reconsider whether you really want to be parsing HTML using regular expressions.

However, what you want is the contents of the third () group in your match. The exec method of a JS regular expression object is an array containing the whole match at index 0, and the matches from all the groups at indices 1,2,... (in this case index 3 is what you need).

[NOTE: an earlier version of this answer had "first" and "1" instead of "third" and "3" above, because I misread your regexp. Sorry.]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜