0

I need to get href argument value, but only for those <link> tags which has type='image/x-icon' attribute (by other words, favicon tags):

// Below ones must be parsed
<link rel="icon" type="image/x-icon" href="#" />
<link rel="shortcut icon" type="image/x-icon" href="#" />

// Below ones must be ignored
<link rel="stylesheet" href="styles.html">
<link rel="search" href="search.html">
<link rel="help" href="help.html">

The problem is type="image/x-icon" could be before href="#" or after it, but exactly one of these cases. How I can express it in regular expression from JavaScript?

Regex fiddle with my try

Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124

3 Answers3

3

You can use DOMParser API instead

let str = `
<link rel="icon" type="image/x-icon" href="#" />
<link rel="shortcut icon" type="image/x-icon" href="#" />


<link rel="stylesheet" href="styles.html">
<link rel="search" href="search.html">
<link rel="help" href="help.html">`

let parser = new DOMParser()
let parsed = parser.parseFromString(str,"text/html")

let links = parsed.getElementsByTagName('link')

console.log(Object.values(links).filter(val=> val.type === 'image/x-icon'))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
1

If you need to do it with a single regex, it's possible to use a lookahead assertion to verify that the link tag contains type="image/x-icon" before going on to capture the href value.

Something like:

/<link (?=[^>]*type="image\/x-icon")[^>]* href="([^"]+)" [^>]*>/

Robert Price
  • 611
  • 4
  • 11
0

You can use an or | in your regex like so:

/<link (?:(?:.* type="image\/x-icon" .* href="([^"]+)" .*)|(?:.* href="([^\"]+)" .* type="image\/x-icon" .*))\/>/

In other words, you have a link tag with either the type attribute followed by the href, or the href attribute followed by the type.

You may need to check the resultant groups and get the one that has a defined value.

David Sampson
  • 727
  • 3
  • 15