0

I am using notepad++ and I want to create an automation in order to replace some strings.

In this case I am going to deal with the a href tag.

So, I will give 3 examples of some lines I have in my code :

01)

<a href="https://url.com" class="logo"><img src="urlurlurlurl" alt=""></a>

02)

<a href="https://url.com" class="logo"><img src="urlurlurlurl" alt="">
         </a>

03)

<a href="https://url.com"><img src="urlurlurlurl" alt=""></a>

04)

<a href="https://url.com">link</a>

So, if I wanted to replace the full a href tag above in all 4 cases, I would use this one : <a href(.*?)a>

Now, I am trying to think of a way to replace the url within the a href tag only.

I tried using that :

href="(?s)(.*?)"|href ="(?s)(.*?)"

and it works fine because I also take into consideration that there might be a space.

But now in the replace window I have to include href=""

enter image description here

Is there a way to make it search for the a href tags and then replace a specific substring of it?

I want to know because there are cases where I have other tags that include a url and I want to replace it. But a generic replacement for all the strings that are included within quotes ("string") would not be good as I do not to replace all of them.

1 Answer 1

1

You can use a negated class to match everything before and after the href like,

(a[^>]*href\s*=\s*")[^"]*

replace with capture group $1REPLACE_STRING

Regex Demo

What it does?

  • a[^>]* Matches a followed by anything other than a closing >.
  • href\s*=\s*" Matches href=". Till here is captured in group 1.
  • [^"]* Matches anything other than ". This form the url that you want to replace.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.