描述
这个正则表达式将:
- 匹配字符串中的最后一个锚标记
- 捕获类属性的值
- 避免在搜索 html 字符串时使用正则表达式的许多潜在问题
.*<a\b(?=\s) # capture the open tag
(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sclass=['"]([^"]*)['"]?) # capture the src attribute value
(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*"\s?\/?> # get the entire tag
![在此处输入图像描述](https://i.stack.imgur.com/TBZtK.png)
例子
现场示例:http ://www.rubular.com/r/G5F6AD5UyL
示例文本
请注意,最后一个标签有一个困难的边缘情况
<a href="#" class="test1"><div class="test2">
<a onmouseover=' class="NotTheClass" ; funClassRotator(class) ; ' class="test3" href="#" ><div class="test4">
捕获组
[0][0] = <a href="#" class="test1"><div class="test2"><a href="#" onmouseover=' class="NotTheClass" ; funClassRotator(class) ; ' class="test3">
[0][1] = test3