If you're trying this client-side, rather than regex, perhaps the easiest modern way would be to use a DOMParser, and then use parseFromString
on the string.
For nodeJS look into cheerio.
If you see the following error:
SyntaxError: `` literal not terminated before end of script
you'll need to escape the the closing /
in the </script>
element first. You'll see if you just add what you have to a JS file it will error out with:
So change that to <\/script>
.
Then you can parse the string, and then pick out its src
and its text content.
const str = `<h1>page 1</h1>
<input type="text" spellcheck="false" data-ms-editor="true">
<script src="/static/test.js">
console.log("hello");
console.log("goodbye");
<\/script>`;
const parser = new DOMParser();
const doc = parser.parseFromString(str, 'text/html');
console.log(doc.querySelector('script').src);
console.log(doc.querySelector('script').textContent);