0

What would be the regex that will catch html element that has class 'slide'? So it will catch:

<div class="slide">
<span class="slide foo">
<img class="foo slide bar">

It shouldn't catch:

<div id="slide">
<div class="slide2">
polmarex
  • 1,363
  • 5
  • 19
  • 35
  • An HTML Parser is the best way to parse HTML. Where is your HTML coming from and what are you running the regex from/in? – Alex K. Jul 12 '17 at 16:59
  • [Don't parse HTML using regex.](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Tom Wyllie Jul 12 '17 at 17:02

1 Answers1

0

I agree that you should avoid parsing HTML with regex... But, if you are stuck in a spot where you have to do it, this should work:

/class="(.*[ ]+)*(slide)([ ]+.*)*"/

Inside the class tag, it is looking for zero or more instances of characters with at least one space before and after the word slide

i-man
  • 558
  • 3
  • 14
  • thank you, do you know maybe why there is an empty string in http://www.phpliveregex.com/p/kIM ( array(4 0 => class="slide ddd" 1 => 2 => slide 3 => ddd ) ? – polmarex Jul 12 '17 at 20:53
  • Yes, there are three capturing groups, and slide will always appear in the second since the first group is for all the class names before 'slide' and the third group is for all the class names that may come after it... even if there are none. – i-man Jul 13 '17 at 20:16