javascriptregexreact-loadable

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


The library (@loadable/server) that I am using is returning <style type='text/css'> <my styles here> </style> and I would like to remove the type='text/css'from there.

The reason is that the W3C validator is raising a warning:

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

I am trying to remove it using regex without success since it is a raw string.

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

I expect to remove from my variable styleTags the type='text/css' by regex or other approach.


Solution

  • I don't know why you want to do that but I guess a simple replace will work :

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

    And if you want to handle the possibility of simple or double quotes you can do :

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