0

I have this regex :

/<iframe[\s\S]*?data-context[\s\S]*?<\/iframe>/ig

and I want to match all iframe that contain the data-context attribute. The problem is that if there's two iframe and the first one don't have the data-context but the second have it, it match from the first to the second one.

Regex101 Demo

Any idea how to make it more "lazy" ?

Jonathan Lafleur
  • 493
  • 5
  • 25

1 Answers1

1

In your pattern you are using only lazy quantifiers [\s\S]*? so in those terms, there are no more quantifiers to make lazy anymore.

What you might do is use a negated character class [^<>]* to not cross matching the angle brackets assuming that they do not appear in between the closing bracket.

The quantifier here does not have to be lazy, as the negated character class can not cross the closing >

This is a brittle solution, this post explains how this can easily break.

<iframe[^<>]*\bdata-context\b[^<>]*>[\s\S]*?<\/iframe>

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70