0

I would like to use regex to get the stylesheets inside it, I use this regex now: /(<link .*href=["'])/gi

but this returns me all <link but I want to filter on link and rel="stylesheet"

could someone help me out on this regex?

Sireini
  • 3,872
  • 10
  • 48
  • 84
  • 1
    Use a parser - [H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – ctwheels Mar 19 '18 at 13:32
  • *inside it* inside what? – revo Mar 19 '18 at 13:34
  • What is the input you are passing? Is it a string? – gurvinder372 Mar 19 '18 at 13:35

2 Answers2

2

but this returns me all

Why not use the built-in querySelectorAll

var links = document.querySelectorAll("link[rel='stylesheet']");
console.log(links.length);

links is the list of link Elements.

Demo

var links = document.querySelectorAll("link[rel='stylesheet']");
console.log(links.length);
<link rel="stylesheet" href="abc.css"/>
<link rel="stylesheet" href="abc2.css"/>
gurvinder372
  • 64,240
  • 8
  • 67
  • 88
0

If I have got you correctly then you are trying to get all links with rel="stylesheet" attribute. To do so you can use following regex

/<link .*rel="stylesheet".*\/>/gi

For demonstrations have a look here.

Rehan Haider
  • 855
  • 13
  • 24