-1

I have an input like the following, where there are two script tags, and the 2nd script tag has src attribute, wherein I need to check if wrapperId={wrapper_id} is present -

<script type="text/javascript"> var blah; </script><script id="bleh" type="text/javascript" src="//domain/test.js?siteid=270606&testmode=0&banner360=1&banner=0&placementid=0&tp=3&wrapperId={wrapper_id}" > </script>

Following is the regex I have prepared so far -

<script(.*?)(?!<\/script>)(.*?)wrapperId={wrapper_id}(.*?)<\/script>

I am trying to match the script tag which does not contain a closing script tag (</script>).

However, if I run it at https://regex101.com/r/oZ2jL6/1, I see it matching the 1st script tag.

If I use PHP preg_match(), the result also says that match was found, but I am sure it is matching the 1st script tag, which I don't want.

Already checked related question -

Regular expression to match a line that doesn't contain a word?

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

1 Answers1

0

I am trying to match the script tag which does not contain a closing script tag ().

In your example you have a closing tag. Regular expressions for matching html are usually not a good idea. Regular expressions for invalid HTML are going to give you headaches.

Assuming a closing tag like your example try:

<script(.*?)<\/script><script(.*?)wrapperId={wrapper_id}(.*?)<\/script>
Robert Smit
  • 118
  • 6