简体   繁体   中英

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=""

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM