admin管理员组

文章数量:1302274

I've a scenario where i have plete web pages having javascript, css and html. I need to remove the script and style tags plus their contents pletely. I have achieved this in PHP using the following regex:

 $str = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html); 
 preg_replace('#<style(.*?)>(.*?)</style>#is', '', $str);

But can't get it done in javascript. I want to have the equivalent of

 <script(.*?)>(.*?)</script> //in javascript

I want to replace all their occurrences within html. I have stripped out the others html tags with this

 pureText.replace(/<(?:.|\n)*?>/gm, ''); //just a reference

I've a scenario where i have plete web pages having javascript, css and html. I need to remove the script and style tags plus their contents pletely. I have achieved this in PHP using the following regex:

 $str = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $html); 
 preg_replace('#<style(.*?)>(.*?)</style>#is', '', $str);

But can't get it done in javascript. I want to have the equivalent of

 <script(.*?)>(.*?)</script> //in javascript

I want to replace all their occurrences within html. I have stripped out the others html tags with this

 pureText.replace(/<(?:.|\n)*?>/gm, ''); //just a reference
Share Improve this question edited Nov 5, 2014 at 14:43 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Oct 24, 2014 at 17:25 Ali BaigAli Baig 3,8674 gold badges35 silver badges49 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

I want to have the equivalent of <script(.*?)>(.*?)</script> //in javascript

/<script([\S\s]*?)>([\S\s]*?)<\/script>/ig

Use [\S\s]*? instead of .*? in your regex because javascript won't support s modifier (DOTALL modifier). [\S\s]*? would match any space or non-space character zero or more times non-greedily.

Don't use regex for this. It is much slower and less reliable than manipulating the DOM.

var scripts = document.getElementsByTagName('script');
var css = document.getElementsByTagName('style');

for(var i = 0; i < scripts.length; i++)
{
    scripts[i].parentItem.removeChild(scripts[i]);
}
for(var j = 0; j < css.length; j++)
{
    css[j].parentItem.removeChild(css[j]);
}

本文标签: htmlRegex To Remove Script And Style TagsContent JavascriptStack Overflow