-1

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.

mplungjan
  • 169,008
  • 28
  • 173
  • 236

2 Answers2

1

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['\"]", '')
Neekobus
  • 1,870
  • 1
  • 14
  • 18
  • @andrehigher You have to match to the quotes used (simple or doubles), or use the second variant that handle both. – Neekobus Jul 11 '19 at 13:16
  • It does not work. `''.replace("type=['\"]text/css['\"]", '')` provides the same string. – andrehigher Jul 11 '19 at 13:17
  • Because .replace does not run on the original string. You need to do `styleTags = styleTags.replace(...)` or `styleTags= getInlineStyleTags().replace(...)` – mplungjan Jul 11 '19 at 13:18
  • @mplungjan I've just added the new variable assignement – Neekobus Jul 11 '19 at 13:18
  • I was thinking about a complex solution using regex but just a replace work well. Thank you @Neekobus – andrehigher Jul 11 '19 at 13:24
  • the second proposition is actually a regex, to handle the 2 types of quotes @andrehigher. You're welcome – Neekobus Jul 11 '19 at 13:52
1

Just replace the string styleTags with ''

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