I have a string let's say headData which is a combination of <script>
, <style>
and <link>
tags. For Ex(with Dummy data) -
let headData = '<style>
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 300;
src: local('Roboto Light'), local('Roboto-Light'), url(path-to.woff) format('woff');
}</style>
<link rel="dns-prefetch" href="//assets.adobedtm.com">
<script>var isPresent = false;</script>
<script>var isContent = true;</script>
<style>@font-face {
font-family: 'Courgette';
font-style: normal;
font-weight: 400;
src: local('Courgette Regular'), local('Courgette-Regular'), url(path-to.woff2) format('woff2');}</style>'
I inject whole of headData in a tag like below.
<script dangerouslySetInnerHTML={{__html: headData}} />
I don't want to inject HTML tags like <style>, <link>
tag related data and only want all the <script>
tag related data to be injected. Is there a way I can achieve this using regex of selecting only <script>
tags.
So what I finally want to inject is similar to -
let headData = '<script>var isPresent = false;</script>
<script>var isContent = true;</script>'
What is the right way to achieve this in Javascript?