在v-html中,js 正则表达式清除字符串里的style属性

时间:2021-11-05 01:01:10

项目中遇到这样的需求,后端返回的是字符串,在vue用v-html显示,里面有style样式,要去除style

在v-html中使用filters,和平时的不一样,推荐项目的方法,定义一个全局的过滤方法

 Vue.prototype.removeHtmlStyle = function (html) {
var rel = /style\s*?=\s*?([‘"])[\s\S]*?\1/
var newHtml = ''
if (html) {
newHtml = html.replace(rel, '')
}
return newHtml
}
 <div class="paragraph" v-html="removeHtmlStyle(articleContent.post_content)">

使用上面的这种方法,就可以了