正则表达式仅返回“链接"标签的"href"属性? [英] RegEx to return 'href' attribute of 'link' tags only?

查看:68
本文介绍了正则表达式仅返回“链接"标签的"href"属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作仅返回<link>标记hrefs

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

为什么此正则表达式返回所有的href,包括< a href?

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


    (?<=<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</a>

谢谢

推荐答案

都可以

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

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

这篇关于正则表达式仅返回“链接"标签的"href"属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆