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);
Source: stackoverflow.com
Related Query
- Include only <script> tag from page source
- How should the script tag in a react page be written to include it properly with webpack?
- How to compress build with/without ejecting create-react-app. Also include compressed file into script tag into index.html
- How to use babel directly from a script tag without installing babel itself
- accessing variable from script tag in html file in react component
- Shopify App - Script Tag - CORS error when retrieving data from store
- Exact same code from dangerouslySetInnerHTML not working only inside Gatsby / ReactJS page
- How to get data from an API only once when the page is loaded using axios.get method?
- Why does data from my api only display once and when I refresh the page it gives an error
- Why it gets the correct data from localStorage only after refreshing the page in React
- How can I include a compiled react js component from a compiled react js single page app?
- Set script tag from API call in header of index.html
- Execute code from inline script that is fetched from another source
- How to add a JS Script tag from into React App's ComponentDidMount?
- React Helmet only loads script on index page but not on other pages
- Script not being executed while navigating, it works only if I refresh the page
- How to create an iframe from an html code snippet that includes a script tag
- React: How do I only render a component on the page once I click on it from a mapped array?
- MDX to add script tag to page
- Webpack configuration for react app to include source maps from other react packages listed as dependencies
- My Link tag from react-router doesn't change my page
- Can't get value from HTML tag attribute "value" to onClick script
- Return only 3 values from a function on one page and all the values on the other page
- Importing script via script tag and using function from that script within react component
- html.js loading the script only once on page load Gatsby
- Remove specific tag from innerHtml and render it on react page
- Only last data is fetched from firesbase after refreshing the page with firebase .on()
- Include Kendo React with Script Tag
- Getting value from a span tag on a web page which built by reactjs
- React tag script are called only the first time
More Query from same tag
- Dynamically change color of an SVG class on click in React?
- React - I have to click a button twice to display api data
- Why are higher-order components a lot more useful than the regular component?
- React.js generate new random number
- Using React.js, Node.js and Multer, how can I console log 'req.file' on the server?
- Webpack hot loader throws "[HMS] Hot Module Replacement is disabled."
- How to horizontally centralize a semantic-ui-react form
- How to use Spacy NLP from within a website
- Creating a generalised onChange() function for different inputs without the use of name tag
- React Child component will mount then unmount after calling parent function to set parent state
- Cannot read properties of undefined (reading 'map') in react js while using redux
- Where to download the latest version of the react-transition group dependency?
- React component not updating from props
- Copy text from a div to clipboard using document.execCommand in React
- React/date-fns - error in component 'Objects are not valid as a React child' when using 'addSuffix'
- Loop through array of objects, if value exists, return another value
- how to get value of promise and use it in selector
- Preview File before uploading in proper way.(React&Typescript)
- How to limit the height of MUI Autocomplete input box
- Redux Thunk Not Dispatching Actions in Order
- Javascript RegExp for exact multiple words with special characters match
- State value doesn't change when I call a function with setstate
- Get access to generated class names in e.g. custom panel header renderer
- How to use `setState` callback on react hooks
- React Native ListView not re-rendering on state change by click
- React: Show different components based on conditions
- React.js - Having trouble creating a new invoice within my project
- Can print out value, but in setState its "undefined"
- How to change default value react-i18next (in localstorage)
- Issue with Adding Components to React Page