Search code examples
htmlregexhref

RegEx to return 'href' attribute of 'link' tags only?


Im trying to craft a regex that only returns <link> tag hrefs

Why does this regex return all hrefs including <a hrefs?

(?&lt;=&lt;link\s+.*?)href\s*=\s*[\'\"][^\'\"]+
<link rel="stylesheet" rev="stylesheet" href="idlecore-tidied.css?T_2_5_0_228" media="screen">
<a href="anotherurl">Slash Boxes&lt;/a>

Solution

  • Either

    /(?<=<link\b[^<>]*?)\bhref=\s*=\s*(?:"[^"]*"|'[^']'|\S+)/
    

    or

    /<link\b[^<>]*?\b(href=\s*=\s*(?:"[^"]*"|'[^']'|\S+))/
    

    The main difference is [^<>]*? instead of .*?. This is because you don't want it to continue the search into other tags.