首页 > 解决方案 > 需要下一个正则表达式仅适用于标签锚

问题描述

我有下一个字符串:

<a href="https://lac.wetlands.org/" class="com-link" target="_blank"><mark class="hl_yellow"><b>Fundación Humedales/Wetlands International:</b></mark></a><mark class="hl_yellow"><b> </b></mark><a href="https://twitter.com/fundachumedales?lang=en" class="com-link" target="_blank">Twitter </a>/ <a href="https://es-la.facebook.com/fundacion.humedales/" class="com-link" target="_blank">Facebook</a> / Instagram. Por el Día de los Humedales, junto al Museo Scasso lanzó <a href="https://www.youtube.com/watch?v=xl0lgozO_HU&amp;feature=youtu.be" class="com-link" target="_blank">este video</a>

我只需要匹配标签 < a > 的所有类。

我有这个正则表达式:

(?:class|className)=(?:["']\W+\s*(?:\w+)()?"'['"]/g;

但它适用于每个标签(如 < 标记 >)。只需要在标签中应用,但我无法得到它。有什么帮助吗?

标签: javascriptregex

解决方案


a这将匹配标签中的类属性:

(?<=<a)(?:.(?!<\/a>))*?class="([^"]*)

演示

关键部分是(?:.(?!<\/a>))*?您在找到之前检查</a>没有发生的地方classe="


推荐阅读