Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74381

Include only tag from page source

$
0
0

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?


Viewing all articles
Browse latest Browse all 74381

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>