替换所有 </script>带有 <\/script> 的标签在 javascript 或 jquery 中

替换所有 </script>带有 <\/script> 的标签在 javascript 或 jquery 中

问题描述:

我需要将所有这些标签 </script> 替换为这些标签 </script>

I need to replace all these tags </script> with these tags </script>

之前:-> <script>...代码...</script>

Before: -> <script>..code..</script> <script>..code..</script>

之后:--->


但这不起作用:


But this does not work:

function myReplace(){
    var X = document.getElementById("demo").innerText;
    var Y = X.replace(/</script>/ig, '<\/script>');
    document.getElementById("demo").innerText = Y;
}

演示

这是相关的帖子为了更好的理解

Here's a related post for a better understanding

看起来你的表达方式不起作用.您的斜线没有正确转义.试试这个.

It looks like your expression isn't going to work. Your slashes aren't being escaped properly. Try this.

function myReplace(){
   var X = document.getElementById("demo").innerHTML;
   var Y = X.replace(/<\/script>/ig, "<\\\/script>");
   document.getElementById("demo").innerText = Y;
}

我还发现了一个 好文章 关于如何做到这一点以及为什么这样做.他们达到了逃避你的 < 的程度.和 > 符号,但我相信转义正斜杠是最重要的.

I also found a good article about how to do this and why. They go to the extent of escaping your < and > sign's, but I believe escaping your your forward slashes is the most important.