如何从字符串中删除 type="text/css" 属性?

How to remove type="text/css" property from string?

我正在使用的库 (@loadable/server) 正在返回 <style type='text/css'> <my styles here> </style>,我想从那里删除 type='text/css'

原因是 W3C 验证器发出警告:

The type attribute for the style element is not needed and should be omitted

我正在尝试使用正则表达式删除它但没有成功,因为它是一个原始字符串。

function getInlineStyleTags() {
   <some logic here>
   ...
   return `<style type='text/css' data-chunk='${asset.chunk}'>${data}</style>`
}
styleTags = getInlineStyleTags()

我希望通过正则表达式或其他方法从我的变量 styleTags 中删除 type='text/css'

我不知道你为什么要这样做,但我想一个简单的 replace 就可以了:

var styleWithoutAttr = styleTags.replace("type='text/css'", '')

如果您想处理单引号或双引号的可能性,您可以这样做:

var styleWithoutAttr = styleTags.replace("type=['\"]text/css['\"]", '')

只需将字符串 styleTags 替换为 ''

styleTags=styleTags.replace(`type='text/css'`,'')