Sireini
Sireini

Reputation: 4262

Regex stylesheet out of string

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?

Upvotes: 0

Views: 61

Answers (2)

Rehan Haider
Rehan Haider

Reputation: 1061

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.

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68433

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"/>

Upvotes: 2

Related Questions