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
2 Answers
Reset to default 7I 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
版权声明:本文标题:html - Regex To Remove Script And Style Tags + Content Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741702056a2393347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论