Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all scripts with javascript regex

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.

like image 484
user1853892 Avatar asked Jan 18 '26 18:01

user1853892


1 Answers

Try this:

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

or

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

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

like image 53
Raheel Hasan Avatar answered Jan 20 '26 08:01

Raheel Hasan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!