Datacrawler
Datacrawler

Reputation: 2876

Replace substring of a string using REGEX in Notepad++

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.

Upvotes: 0

Views: 4417

Answers (1)

nu11p01n73R
nu11p01n73R

Reputation: 26667

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.

Upvotes: 1

Related Questions