具有所有属性的锚标记的正则表达式
我正在尝试获取一个正则表达式来替换文本字符串中的所有链接作为链接的值。
链接可能如下所示:
<a href="http://whatever" id="an_id" rel="a_rel">the link</a>
<a href="/absolute_url/whatever" id="an_id" rel="a_rel">the link</a>
我想要一个我得到的正则表达式:链接
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它远非完美,但您需要提供更多示例来说明什么是正确匹配,什么不是(例如空格呢?)
It's far from being perfect, but you need to provide more examples of what is a correct match and what isn't (e.g. what about whitespaces?)
该标签将匹配任何
...
标签,包括正确匹配包含...
标签的标签。或任何完整标签,例如:将捕获:
href="test.html"
该行包含一个 HTML 开头 <括号。
并
href="test.html"
该行包含粗体文本。
"" 等)和包含(标签之间的内容),如果不需要它们,可以将其删除。
如果要跨多行捕获,请在末尾的“g”标志之前或之后添加“s”。请注意,“s”标志可能不适用于所有类型的正则表达式。
捕获示例(不使用“s”标志 - regexr 尚不支持):http://regexr.com/39rsv
This one will match any
<a ...>...</a>
tag including correctly matching ones that contain a < or any full tags such as:Would capture:
href="test.html"
This line contains an HTML opening < bracket.
and
href="test.html"
This line contains <strong>bold</strong> text.
It also includes capturing groups for the tag attributes (like class="", href="", etc) and contain (what is between the tag) that can be removed if you do not need them.
If you want to capture across multiple lines add an "s" before or after the "g" flag at the end. Note that the "s" flag may not work in all flavors of regular expression.
Capture example (not using the "s" flag - not supported by regexr yet): http://regexr.com/39rsv
只是对已接受的答案进行了一些修正。这是正确的正则表达式:
/]*>([^<]+)<\/a>/g
。用于关闭锚标记的正斜杠
(/)
未转义,因此不会进行匹配。Just a little correction from the accepted answer. This is the correct regex:
/<a[^>]*>([^<]+)<\/a>/g
. The forward slash(/)
for closing the anchor tag</a>
was not escaped so no match will be made.我刚刚添加了显式命名的组:
https://regex101.com/r/ sbtcYr/1
I just added explicitly named groups:
https://regex101.com/r/sbtcYr/1
我无法获得此处列出的任何答案......不确定他们是否正确理解了您的问题。
我阅读您的帖子的方式是您正在寻找
example tag
之间的内容(又名提取“example tag”)
但是我设法想出这个解决方案。它似乎并不在所有浏览器中工作,尽管这是一个无赖(又名边缘,IE,还没有尝试过 FF)
此链接显示它工作
https://regexr.com/5dd0m
I was not able to get any of the answers listed here to work...not sure they read your question right.
The way I read your post you're looking for the INBETWEEN of the
<a href="abcdefg">example tag</a>
(aka extract "example tag")
However I managed to come up with this solution. It doesn't appear to work in all browsers though which is a bummer (aka edge, IE, haven't tried FF)
This link shows it working
https://regexr.com/5dd0m
试试这个 100% 的工作
(?i)
try this 100% work
(?i)<a(.*)(")>
像这样的东西应该足够了
Something like this should be enough