简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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