0

Now REGULAR EXPRESSION

<a\s(class|href|target)=\"(.*)\"\s(class|href|target)=\"(.*)\"\s(class|href|target)=\"(.*)\">(.*)<\/a>/g

MATCH THIS LINK

<a href="index.php" target="5454 target 54" class="nav">test</a>

But i want to use only one regex using conditions match every url

example:

  1. <a href="index.php">cal</a>

  2. <a class="xxx" href="index.php">cal</a>

  3. <a class="navbar-brand" href="index.php" target="">cal<span>.net</span></a>

I test this in: https://regex101.com

and GET MATHCH INFO LIKE THIS

1 Answers1

2

You can use this (demo):

<a
    \s*(?:(class|href|target)=\"(.*?)\")?
    \s*(?:(class|href|target)=\"(.*?)\")?
    \s*(class|href|target)=\"(.*?)\"
    \s*
>
    (.*?)
<\/a>

It uses optional non capturing groups: (?:)? and lazy quantifiers *?. The output is:

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>`
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142