user3325376 user3325376 - 6 months ago 8
PHP Question

regex not stop when using dot matches

I want to remove some scripts from pages that contains "site" word

<scritp>
o.com
bla bla bla
</script><p>this is line></p>

<script>
google.com/jquery.js !
</script>

<scritp>
site.com
bla bla bla
</script><p>aa</p>

CONTENT
STYLE
SIDEBAR
...


<scritp>
site.com
aaa bla bla bla
</script><p>a</p>


i used this regex

<scritp>.*?site.*?<\/script>


but unfortunately it contains lines do not related to the matches i want

debug link : https://regex101.com/r/rC0vF8/2

how can i stop regex when it's find
</script>


p.s : i want to match all
<script>site.com</script>
at once

Answer

Confusing looks, that you have some scritp and some script in your sample & demo. Is this meant? However you can use a negative lookahead if this would be convenient for your input:

<script>((?!</script).)*?site(?1)*</script>
  • ((?!</script).)*? matches lazily any amount of characters not followed by </script
  • until site and (?1) reuses the pattern in first group until </script> greedily.

More explanation and demo at regex101

For this kind of problems usually a parser solution is be to be preferred. Depends on input.

Comments