使用JS正则表达式从html中删除所有脚本标记 [英] Removing all script tags from html with JS Regular Expression

查看:366
本文介绍了使用JS正则表达式从html中删除所有脚本标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在pastebin中去掉这个html标签

mdxygM0arel =noreferrer> http://pastebin.com/mdxygM0a


我试过使用下面的常规表达

  html.replace(/< script。*>。*< \ / script> / ims, )

但它不会删除html中的所有脚本标记。它只会删除内联脚本。请我需要一个正则表达式,可以删除所有脚本标记(内联和多行)。如果在我的示例 http://pastebin.com/mdxygM0a 上执行测试, p>

谢谢 解决方案

试图使用正则表达式去除HTML标记是有问题的。您不知道脚本或属性值是什么。一种方法是将它作为div的innerHTML插入,删除任何脚本元素并返回innerHTML,例如

 函数stripScripts( s){
var div = document.createElement('div');
div.innerHTML = s;
var scripts = div.getElementsByTagName('script');
var i = scripts.length;
while(i--){
scripts [i] .parentNode.removeChild(scripts [i]);
}
return div.innerHTML;

$ b alert(
stripScripts('< span>< script type =text / javascript> alert(\'foo \');< ; \ / script>< \ / span>')
);

请注意,目前,如果使用innerHTML属性插入,浏览器将不会执行脚本,尤其是元素没有添加到文档中。


i want to strip script tags out of this html at pastebin

http://pastebin.com/mdxygM0a

I tried using the below regular expression

html.replace(/<script.*>.*<\/script>/ims, " ")

But it does not remove all script tags in the html. It only removes in-line scripts. Please i need a regex that can remove all script tags(in-line and multi-line). It would be highly appreciated if a test is carried out on my sample http://pastebin.com/mdxygM0a

Thanks

解决方案

Attempting to remove HTML markup using a regular expression is problematic. You don't know what's in there as script or attribute values. One way is to insert it as the innerHTML of a div, remove any script elements and return the innerHTML, e.g.

  function stripScripts(s) {
    var div = document.createElement('div');
    div.innerHTML = s;
    var scripts = div.getElementsByTagName('script');
    var i = scripts.length;
    while (i--) {
      scripts[i].parentNode.removeChild(scripts[i]);
    }
    return div.innerHTML;
  }

alert(
 stripScripts('<span><script type="text/javascript">alert(\'foo\');<\/script><\/span>')
);

Note that at present, browsers will not execute the script if inserted using the innerHTML property, and likely never will especially as the element is not added to the document.

这篇关于使用JS正则表达式从html中删除所有脚本标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆