现在是正则表达式
<a\s(class|href|target)=\"(.*)\"\s(class|href|target)=\"(.*)\"\s(class|href|target)=\"(.*)\">(.*)<\/a>/g匹配此链接
<a href="index.php" target="5454 target 54" class="nav">test</a>但我只想使用一个正则表达式,使用条件匹配每个url。
示例:
<a href="index.php">cal</a><a class="xxx" href="index.php">cal</a><a class="navbar-brand" href="index.php" target="">cal<span>.net</span></a>我在以下方面进行了测试:https://regex101.com
发布于 2016-07-19 11:14:44
您可以使用这个(演示):
<a
\s*(?:(class|href|target)=\"(.*?)\")?
\s*(?:(class|href|target)=\"(.*?)\")?
\s*(class|href|target)=\"(.*?)\"
\s*
>
(.*?)
<\/a>它使用可选的非捕获组:(?:)?和惰性量词*?。产出如下:
MATCH 1
5. [3-7] `href`
6. [9-18] `index.php`
7. [20-23] `cal`
MATCH 2
1. [31-36] `class`
2. [38-41] `xxx`
5. [43-47] `href`
6. [49-58] `index.php`
7. [60-63] `cal`
MATCH 3
1. [71-76] `class`
2. [78-90] `navbar-brand`
3. [92-96] `href`
4. [98-107] `index.php`
5. [109-115] `target`
6. [117-117] ``
7. [119-139] `cal<span>.net</span>`https://stackoverflow.com/questions/38454755
复制相似问题