简体   繁体   English

从字符串中删除无效的html标签

[英]Remove invalid html tags from a string

Is there any way with javascript to remove invalid HTML tags from a string? javascript有什么方法可以从字符串中删除无效的HTML标签?

Ex: <div> <foo></foo> </div> should be transformed to <div></div> 例如: <div> <foo></foo> </div>应该转换为<div></div>

It looks like you're trying to roll your own form of HTML sanitizer. 看来您正在尝试使用自己的HTML清理程序形式。 I would not recommend that. 我不建议这样做。 Instead, check out other questions that have already asked for a sanitizer: Sanitize/Rewrite HTML on the Client Side 相反,请检查其他已经要求使用消毒剂的问题: 在客户机上消毒/重写HTML

Specifically, there's one available for use here: https://code.google.com/p/google-caja/wiki/JsHtmlSanitizer 具体来说,可在此处使用一种: https : //code.google.com/p/google-caja/wiki/JsHtmlSanitizer

Try this: 尝试这个:

<!DOCTYPE html>
<html>
<body>

<p id="demo"><foo></foo></p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var str=document.getElementById("demo").innerHTML, 
    reg = /<foo>|<\/foo>/i;

str = str.replace(reg, " "); 
document.getElementById("demo").innerHTML=str;
}
</script>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM