简体   繁体   中英

Regex in NP++ to replace multiple queries with different ones

I use Notepad++ to search and replace using regular expressions, and I need to replace more than one line with something else each.. Can I do that with one command ?

Example:

"../../../commons/js/site-name.com__file-name.js"
"../dlds/file.css"

or

anytext "../../../commons/js/site-name.com__file-name.js" text
something "../dlds/file.css" anytext

or

<script src="../../../Folder/js/site-name.com__file-name.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/folder/file.css" />

output should be

"/resources/common.js"
"/resources/common.css"

Given that:

  • The up dir ../ are changing (some are one ../ some more ../../../ etc., so I need it to do that for any number of ../), or it could totally missing like: "/dlds/file.css".
  • Those two lines in the example do not come exactly after each other, or at the beginning of text, but anywhere in the text.

search for ^.+/(filename.js|style.css)"$

Replace with "/resources/$1"

If you want to get any filename not just filename.js or style.css, use this

^.+\/(.+)(?!\/)"$

and use the same replacement.

Ex: http://rubular.com/r/h1IsZK8nry

Find:

\.\..+/([^\.]+\.\w+)

Replace:

/resources/\1

Update:

If you want to just replace filename.js or style.css find:

[\./]{1}.+/(filename.js|style.css)

replace with

/resources/\1

How about:

search for : "(?:\\.\\./)+.*/([^.]+\\.\\w+)"
replace with : "/ressources/$1"

if you want to replace with new.js or new.css do:

search for : "(?:\\.\\./)+.*/[^.]+(\\.\\w+)"
replace with : "/ressources/new$1"

for match also urls that begin with / :

search for : "(?:(?:\\.\\./)+.*?)?/([^.]+\\.\\w+)"
replace with : "/ressources/new$1"

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