score:0

i am not familiar with react, but it is generally not a good idea to try to parse html using regular expressions.

you could run into all kinds of problems with regular expressions. (for example, some of the script tags could contain code like this: <script> const mystring='<script></script>'; </script>).

i would suggest using the browser's built-in parser rather than regular expressions to extract the script tags and their content.

function getscriptsstring(headstring) {
  const head = document.createelement('head');
  head.innerhtml = headdata;
  const headchildrenarray = array.from(head.children);
  const scriptsstring = headchildrenarray.reduce((str,el) => {
    if(el.tagname === 'script') {
      return str + el.outerhtml;
    }
    return str;
  }, '');
  return scriptsstring;
}

score:1

you can find the wanted tags with regex capturing groups and match():

/(<script>)[^<>]*(<\/script>)/g

demo:

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>`;
            
 var re = /(<script>)[^<>]*(<\/script>)/g;
 headdata = headdata.match(re).join('\n');
 console.log(headdata);
 


Related Query

More Query from same tag