从字符串中删除 <script> 标记以及它们之间的内容

Remove <script> tags from a string along with the content between them

本文关键字:之间 删除 字符串 script      更新时间:2023-09-26

我看了又看,但找不到任何东西。 所以假设我有这个字符串...

var str = '<script>blah blah blah</script><a>...</a><div>...</div><p>...</p>';

我需要从字符串中删除脚本标签以及标签之间的所有内容。 剥离脚本标签后,我需要将其附加到文本区域。 我到底如何使用jQuery做到这一点?

任何帮助将不胜感激!

由于没有其他人会发布一个简单且可靠的例程,我想我会:

function noscript(strCode){
   var html = $(strCode.bold()); 
   html.find('script').remove();
 return html.html();
}

alert(   noscript("<script>blah blah blah</script><div>blah blah blah</div>")    );
//shows: "<div>blah blah blah</div>"

试试这个:-

var str= "<script>blah blah blah</script><div>blah blah blah</div>";
var html = $(str.bold()); 
html.find('script').remove();

像这样:

var div = $('<div>').html(str);
div.find('script').remove();
str = div.html();

示例:http://codepen.io/paulroub/pen/IabsE

这是答案

的小提琴(https://jsfiddle.net/skoley/9dnmbj3d/),最好从其他几个来源获取,归功于他们。

function noscript(strCode){
   var html = $(strCode.italics()); 
   html.find('script').remove();
 return html.html();
}
function noscript_alt(str)
{
var div = $('<div>').html(str);
div.find('script').remove();
str = div.html();
return str;}