'regex to find script and stylesheet tags

I am trying to get all script tags and link tags for stylesheets only. Right now I have this:

var re = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;

Text:

<link rel="preload" href="fonts/roboto-v27-latin/roboto-v27-latin-700.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="css/site.css" as="style">
<link rel="stylesheet" href="css/site.css" media="screen">
<script src="plugins/glider-js/glider.min.js"></script>

It matches the js file. But how to get the tag for site.css too? This is what I am trying to do:

var re = /<script\b[^>]*>([\s\S]*?)<\/script>|<link rel="stylesheet">/gm;


Solution 1:[1]

If you search for the = + stylesheet you should find all your links with few false positives. We make sure that we don't include any </script>tag before the last one.
If there are nested script tags this will return the inner pair.
I'm assuming that all of your tags are lower case : <script> and not <SCRIPT>. We could write the regex to capture both if there is a risk of missing valid links.

<script((?!(<(\/)script))[\s\S])*\=['\"]?(stylesheet)['\"]?((?!(<(\/)script))[\s\S])*<\/script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1