There are several problems with that regex, the worst of them being that you seem to be mixing up capturing and non-capturing groups. As Mike Samuel hinted, the third capturing group is the (\s*?)
at the very end (which, like the one at the beginning, served no useful purpose). Try this regex:
/<span\b[^>]*\bclass=\s*(['"]?)forbes_entity\1[^>]*>[\s\S]*?<\/span>/ig
Here there's only the one capturing group; it captures a single-quote, a double-quote, or nothing. After the class name, the \1
matches the same thing again. (I changed the class name to match the sample text in your fiddle.)
It turned out I didn't need any other groups, but if I had needed them I would have used non-capturing groups ( (?:...)
) to make it easier to keep track of the capturing-group numbers. I also used [\s\S]
instead of .
to match the span's contents, in case it contains any newlines.