具有所有属性的锚标记的正则表达式

发布于 2025-01-03 00:52:36 字数 280 浏览 0 评论 0 原文

我正在尝试获取一个正则表达式来替换文本字符串中的所有链接作为链接的值。

链接可能如下所示:

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

我想要一个我得到的正则表达式:链接

I'm trying to get a regular expression to replace all the links out of a text string for the value of the link.

A link may look like these:

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

I want a regular expression that I get: the link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

滿滿的愛 2025-01-10 00:52:36
/<a[^>]*>([^<]+)<\/a>/g

它远非完美,但您需要提供更多示例来说明什么是正确匹配,什么不是(例如空格呢?)

/<a[^>]*>([^<]+)<\/a>/g

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?)

染柒℉ 2025-01-10 00:52:36
/<a[\s]+([^>]+)>((?:.(?!\<\/a\>))*.)<\/a>/g

该标签将匹配任何 ... 标签,包括正确匹配包含 ... 标签的标签。或任何完整标签,例如:

blah blah <a href="test.html">This line contains an HTML opening < bracket.</a> blah blah
blah blah <a href="test.html">This line contains <strong>bold</strong> text.</a> blah blah

将捕获:

<a href="test.html">This line contains an HTML opening < bracket.</a>
  • 带有捕获组:
    • href="test.html"
    • 该行包含一个 HTML 开头 <括号。

<a href="test.html">This line contains <strong>bold</strong> text.</a>
  • 带有捕获组:
    • href="test.html"
    • 该行包含粗体文本。

"" 等)和包含(标签之间的内容),如果不需要它们,可以将其删除。

如果要跨多行捕获,请在末尾的“g”标志之前或之后添加“s”。请注意,“s”标志可能不适用于所有类型的正则表达式。

捕获示例(不使用“s”标志 - regexr 尚不支持):http://regexr.com/39rsv

/<a[\s]+([^>]+)>((?:.(?!\<\/a\>))*.)<\/a>/g

This one will match any <a ...>...</a> tag including correctly matching ones that contain a < or any full tags such as:

blah blah <a href="test.html">This line contains an HTML opening < bracket.</a> blah blah
blah blah <a href="test.html">This line contains <strong>bold</strong> text.</a> blah blah

Would capture:

<a href="test.html">This line contains an HTML opening < bracket.</a>
  • with capture groups:
    • href="test.html"
    • This line contains an HTML opening < bracket.

and

<a href="test.html">This line contains <strong>bold</strong> text.</a>
  • with capture groups:
    • 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

各自安好 2025-01-10 00:52:36

只是对已接受的答案进行了一些修正。这是正确的正则表达式:/]*>([^<]+)<\/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.

戈亓 2025-01-10 00:52:36

我刚刚添加了显式命名的组

<a.*href\s?=['"]*(?<href>[^'"]*)[^>]*>((?<text>(.(?!\<\/a\>))*.))<\/a>

https://regex101.com/r/ sbtcYr/1

I just added explicitly named groups:

<a.*href\s?=['"]*(?<href>[^'"]*)[^>]*>((?<text>(.(?!\<\/a\>))*.))<\/a>

https://regex101.com/r/sbtcYr/1

盗琴音 2025-01-10 00:52:36

我无法获得此处列出的任何答案......不确定他们是否正确理解了您的问题。

我阅读您的帖子的方式是您正在寻找 example tag 之间的内容

(又名提取“example tag”)

但是我设法想出这个解决方案。它似乎并不在所有浏览器中工作,尽管这是一个无赖(又名边缘,IE,还没有尝试过 FF)

此链接显示它工作
https://regexr.com/5dd0m

(?<=<a.*>).+(?=<\/a>)

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

(?<=<a.*>).+(?=<\/a>)
大海や 2025-01-10 00:52:36

试试这个 100% 的工作

(?i)

try this 100% work

(?i)<a(.*)(")>

喜爱皱眉﹌ 2025-01-10 00:52:36

像这样的东西应该足够了

<a.*?>(.*)?</a>

Something like this should be enough

<a.*?>(.*)?</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文