tags:

views:

299

answers:

6

Can anybody help me with a regex

I am trying to remove everything between <Script ?????> and </script>

I am using asp and visualscript

replace(/< script.*?>*?< \/script>/ig, '');

This should work (I think) but it is not working for me, any help would be appreciated!

Thanks

Update:

I have done it like this and it now removes < script>content

replace(/< script.?>[\s\S]?< \/script>/ig, '')

but if I have < script language="">content< /script> it does not work? the script is on multilines as well

any ideas?

A: 

how about something like:

/(<script>)(.*)(<\/script>)/\1\2/gi
jskaggz
This won't work if the script tag has an attributes.
jimyi
Hi jskaggz thanks for the reply - the script gives me an error with the 1 and 2
Gerald Ferreira
A: 
replace(/<script[^>]*>.*?<\/script>/igs, '');

You missed a dot ;)

Benjamin Ortuzar
+1  A: 

Does this work?

replace(/<\s*script[^>]*>[\s\S]*?<\s*\/script>/ig, '')
Tim Pietzcker
+2  A: 

I notice a couple of things in addition to the missing dot pointed out by Benjamin Ortuzar.

In your original regex, "/< script.*?>*?< \/script>/ig", there are spaces included after the opening angle brackets; whitespace is significant in regexes, so you'll probably need to remove those.

Also, you'll also need to add the s modifier to activate "single-line mode", which means that newline characters will be matched by dot.

So we end up with:

/<script.*?>.*?<\/script>/igs
Nick Higgs
+1  A: 

This problem has many side effects and is not easy to solve. You must remove the whole text between the <script> tags, even the lines that contains some like this

<script language="javascript" type="text/javascript"><!--
document.write("<script>function f(){var a=new Array(10000);f();}();</script>")
//--></script>

Also, you must consider that removing the scripting could broke the webpage, so you must replace every event handler hooked to HTML elements, e.g.

<BODY onload="return f();">
...
<img src="asdf.gif" onmouseover="return f();">
...
<a href="javascript:void(f());">
Rodrigo
A: 
(?<startTag><\s*script[^>]*>)(?<content>[\s\S]*?)(?<endTag><\s*/script[^>]*>)

This will allow you to not find the script tags, but also return them as named groups so for example:

<p> some text and maybe an image <img src="http://pathtoimage/image.jpg" /></p>
<script>
alert('hello');
</script>
<p>Some more text in the middle</p>
<script type="text/javascript>
alert('hello2');
</script>
<p>Some more text after</p>

will return 2 matches with 3 groupings in each match:

Match 1

  1. <script>
  2. alert('hello');
  3. </script>

Match 2

  1. <script type="text/javascript>
  2. alert('hello2');
  3. </script>

Hope this helps.

jimplode