3

I am trying to remove all scripts tags with content from the string of the type below with regex in javascript. But I am still getting as output:

");</script>

when it should be an empty string.

The code is:

var BG = '<script type="text/javascript">document.write("<script type=\"text\/javascript\" src=\"http:\/\/site;js=y;target=_blank;time="+ (window.emediate_time ? window.emediate_time : window.emediate_time = new Date().getTime()) +"1053997930;"><\/script>");</script><script type="text/javascript" src="some?cre=mu;js=y;target=_blank"></script>';

BG = BG.replace(/<\s*script.*?>.*?(<\s*\/script.*?>|$)/ig,'');

Could you please tell me what's wrong and how to fix it. Thanks.

user1853892
  • 107
  • 2
  • 5

1 Answers1

7

Try this:

(/<.*?script.*?>.*?<\/.*?script.*?>/igm, '')

or

(/<script.*?>.*?<\/script>/igm, '')

(you need 'm' switch to search multi-line)

Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70
  • you need 'm' switch to search multiline. – Raheel Hasan Apr 04 '13 at 16:39
  • There is no need for multiline mode, and a script tag must start with ` – MikeM Apr 04 '13 at 19:45
  • 1
    You are not understanding multiline mode correctly. It is only used when a regex contains `^` or `$`, and you want them to match the start and end of a line, rather than of the whole string. – MikeM Apr 05 '13 at 12:10