正在从html数据中删除脚本。。。使用javascript

正在从html数据中删除脚本。。。使用javascript,javascript,jquery,Javascript,Jquery,所以我有这个函数来从页面中删除脚本,但是一些有很多行的脚本仍然显示出来。是否有方法从加载的页面中删除所有脚本 function filterData(data){ // filter all the nasties out // no body tags data = data.replace(/<?\/body[^>]*>/g,''); // no linebreaks data = data.replace(/[\r|\n]+/g,''); // no comments

所以我有这个函数来从页面中删除脚本,但是一些有很多行的脚本仍然显示出来。是否有方法从加载的页面中删除所有脚本

 function filterData(data){

// filter all the nasties out
// no body tags
data = data.replace(/<?\/body[^>]*>/g,'');
// no linebreaks
data = data.replace(/[\r|\n]+/g,'');
// no comments
data = data.replace(/<--[\S\s]*?-->/g,'');
// no noscript blocks
data = data.replace(/<noscript[^>]*>[\S\s]*?<\/noscript>/g,'');
// no script blocks
data = data.replace(/<script[^>]*>[\S\s]*?<\/script>/g,'');
// no self closing scripts
data = data.replace(/<script.*\/>/,'');

// [... add as needed ...]
return data;
  }
函数过滤器数据(数据){
//过滤掉所有的污渍
//没有身体标签
data=data.replace(/checkout sugar.js-

它有一个removeTags方法,可以执行您想要的操作


如果我没弄错,您需要从HTML字符串中删除所有带有内部代码的
标记。在这种情况下,您可以尝试以下正则表达式:

data.replace(/<script.*?>[\s\S]*?<\/script>/ig, "");
data.replace(/[\s\s]*?/ig,”);
它应该能够成功地使用一行程序和多行程序,并且不会影响其他标记

演示:

函数过滤器数据(数据){
var root=document.createElement(“主体”);
root.innerHTML=数据;
$(root).find(“script,noscript”).remove();
函数removeAttrs(节点){
$.each(node.attributes,函数(index,attr){
if(attr.name.toLowerCase().indexOf(“on”)==0){
node.removeAttribute(属性名称);
}
});
}
函数漫游(根){
除去根;
$(root.childNodes)。每个(函数(){
if(this.nodeType==3){
if(!$.trim(this.nodeValue).length){
$(this.remove();
}
}
else if(this.nodeType==8){
$(this.remove();
}
else if(this.nodeType==1){
走(这个);
}
});
}
走(根);
返回root.innerHTML;
}
filterData(“警报('hello');hello\n\n”);
//“你好”

var-ccKeywords=“keyword=”;if(typeof(ccauds)!='undefined'){for(var-cci=0;cci0)ccKeywords+=”&keyword=“;ccKeywords+=ccauds.Profile.viewers.viewers[cci].abbr;}}Blynn,请把它放在你的原始问题中,而不是评论中。我必须承认我只在这种情况下使用过jQuery…
data=$(“+data+”).find('script').remove().end().html()
data.replace(/<script.*?>[\s\S]*?<\/script>/ig, "");
function filterData(data){
    var root = document.createElement("body");
    root.innerHTML = data;

    $(root).find("script,noscript").remove();

    function removeAttrs( node ) {
        $.each( node.attributes, function( index, attr ) {
            if( attr.name.toLowerCase().indexOf("on") === 0 ) {
                node.removeAttribute(attr.name);
            }
        });
    }

    function walk( root ) {
        removeAttrs(root);
        $( root.childNodes ).each( function() {
            if( this.nodeType === 3 ) {
                if( !$.trim( this.nodeValue ).length ) {
                    $(this).remove();
                }
            }
            else if( this.nodeType === 8 ) {
                $(this).remove();
            }
            else if( this.nodeType === 1 ) {
                walk(this);
            }
        });
    }

    walk(root);

    return root.innerHTML; 
}

filterData("<script>alert('hello');</script></noscript></script><div onclick='alert'>hello</div>\n\n<!-- comment -->");
//"<div>hello</div>"