去掉Html的注释及meta标签

有如下代码:

<!--headTrap<body></body><head></head><html></html>-->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="format-detection" content="telephone=no">


        
        




        <link rel="dns-prefetch" href="//res.wx.qq.com">
<link rel="dns-prefetch" href="//mmbiz.qpic.cn">
<link rel="shortcut icon" type="image/x-icon" href="//res.wx.qq.com/mmbizwap/en_US/htmledition/images/icon/common/favicon22c41c.ico">


        <title>最新发布: 企业自主环保验收管理的指导意见!8种情况可通过验收,4种不予通过</title>
        


<!--[if lt IE 9]>
<link onerror="wx_loaderror(this)" rel="stylesheet" type="text/css" href="//res.wx.qq.com/mmbizwap/en_US/htmledition/style/page/appmsg/page_mp_article_improve_pc2c9cd7.css">
<![endif]-->
<body>
    <div>
        测试测试
    </div>
</body>

怎么通过正则将<!---->注释、meta标签、link标签都替换为""(空字符串)

阅读 7.5k
1 个回答

最直观的写法

str.replace(/(<!--.*?-->)|(<meta.*?>)|(<link.*?>)/g, '')

稍微简化下

str.replace(/(<!--.*?-->)|(<(meta|link).*?>)/g, '')
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题