这应该适合你:
var stringOfHtml = // your string here
$(stringOfHtml).find('script').remove();
要删除删除了脚本标记的新字符串:
var stringOfHtml = "
var html = $(stringOfHtml);
html.find('script').remove();
var stringWithoutScripts = html.wrap("
").parent().html(); // have to wrap for html to get the outer element
';
JS Fiddle Example - 不得不使用p代替脚本,因为脚本打破了小提琴,但同样的原则。
这里的实际工作答案(希望如此)
以下是脚本问题的解决方法,使用replace将脚本文本与其他内容交换(尝试使其唯一)然后删除这些新标记并再次使用替换以换回以防脚本在文本中的任何其他位置使用。是的,它确实使用了正则表达式,但没有删除脚本标签,所以我希望这没关系;):
var stringOfHtml = "
var noScript = wrappedString.replace(/script/g, "THISISNOTASCRIPTREALLY");
var html = $(noScript);
html.find('THISISNOTASCRIPTREALLY').remove();
alert(html.html().replace(/THISISNOTASCRIPTREALLY/g, 'script'));