5年前
⋅
3
<!DOCTYPE html>
<html>
<head>
<title>过滤HTML只保留指定的标签比如p,img,br</title>
<meta charset='utf-8'/>
</head>
<body>
<script type="text/javascript">
var rt = '<p> 123<br/>46</p><IMG src="https://avatar-static.segmentfault.com/309/105/3091058249-1030000000476139_big64"/><p>';//需要过虑的内容
rt = rt.replace(/<\/?(?!p|P|img|IMG|Img|br|BR|Br)[^>]*>/g, ''); // 去除HTML tag,保留p,img,br标签
rt = rt.replace(/[ | ]*\n/g, '\n'); // 去除行尾空白
rt = rt.replace(/ /ig, '');// 去掉
console.log(rt);
</script>
</body>
</html>