2

I want to find all places which start with href=" and ends with " Matches:

<li><a href="Blah/Index.html">Blah</a></li>
<li><a href="/blah/Index.html">Blah</a></li>
<li><a href="../Blah/index.html">Blah</a></li>
<li><a href="../Foo/Index.html">Foo</a></li>

The expected result after the rename

<li><a href="blah/index.html">Blah</a></li>
<li><a href="/blah/index.html">Blah</a></li>
<li><a href="../blah/index.html">Blah</a></li>
<li><a href="../foo/index.html">Foo</a></li>

My regex expression which I build doesn't work:

^href".*\."$

And I have no idea if its possible to change the all found occurrences to the lowercase using global search in VS Code? If it's not possible can you give me some alternative, please? Cheers

GoldenAge
  • 2,918
  • 5
  • 25
  • 63
  • 1
    In NPP, use something like `(href=")([^"]*\.[^".]+)(")` and replace with `$1\L$2\E$3`. See [this demo](https://regex101.com/r/Z06EWl/1). If you do not need to check if the href value contains a dot, the regex can be simplified to `(href=")([^"]+)(")` – Wiktor Stribiżew Dec 08 '18 at 20:47
  • Works as I wanted, ta! – GoldenAge Dec 09 '18 at 14:31

2 Answers2

5

Visual Studio Code regex does not support case modifying operators in the replacement pattern.

In Notepad++, you may use something like

Find: (href=")([^"]*\.[^".]+)(")
Replace: $1\L$2\E$3

See this demo.

Details

  • (href=") - Group 1: href=" substring
  • ([^"]*\.[^".]+) - Group 2:
    • [^"]* - 0+ chars other than "
    • \. - a dot
    • [^".]+ - 1+ chars other than " and .
  • (") - Group 3: a double quotation mark.

The $1\L$2\E$3 replacement pattern inserts back Group 1 value, then \L operator tells the regex engine to turn on lowercasing, $2 is thus inserted in lower case, \E stops lower case output, and $3 adds the Group 3 value.

If you do not need to check if the href value contains a dot, the regex can be simplified to (href=")([^"]+)(").

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Hey @Wiktor. I have a simillar problem here and maybe you can help me? I need to select everything that starts with `${langVars.` and ends with `|escape}` The goal ist to replace the starting and the ending phrase. here is an example of the current var: `${langVars.VAR_LOREMIPSUM|escape}` and this is how it should be after the search and replace: `${text('VAR_LOREMIPSUM')}` thanks – nonnnnn Nov 29 '20 at 16:28
  • @nonnnnn `\$\{langVars\.(.*?)\|escape\}` => `${text('$1')}`, see [this regex demo](https://regex101.com/r/ps1t6h/1). – Wiktor Stribiżew Nov 29 '20 at 21:59
2

You can try this

\bhref="([^>]*)

Explanation

  • \b - Word boundry.
  • href=" Matches href=".
  • ([^>]+) - Match anything one or more time except >.

Demo

Working code demo

const regex = /\bhref="([^>]+)/gm;
const str = `<li><a href="Blah/Index.html">Blah</a></li>
<li><a href="/blah/Index.html">Blah</a></li>
<li><a href="../Blah/index.html">Blah</a></li>`;

let op = str.replace(regex,(match,p1)=>p1.toLowerCase());
console.log(op);

Well in case you want to do replacement also vscode itself you can follow this:-

  • Press Ctrl+H.
  • Press Alt+R.
  • Type your regex.
  • Press Tab and type $1.
  • Press Alt+Enter.
  • Press F1 and type lower, then press Enter.
  • Press Ctrl+Alt+Enter.
slava
  • 791
  • 1
  • 11
  • 26
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • The regex expression works in VS Code but what about changing all found occurrences to lowercase using the Global Search in VS Code? – GoldenAge Dec 08 '18 at 17:14
  • @GoldenAge updated answer for replace too. you can check. – Code Maniac Dec 08 '18 at 17:37
  • can you add some description to each step please, im trying to apply your idea following the steps and have some issues – GoldenAge Dec 08 '18 at 18:08
  • @GoldenAge it's working i tried it in my vscode. what problem you're facing. – Code Maniac Dec 08 '18 at 18:09
  • Ok so Ill tell you all the steps: 1. I open my project in VS Code and I click Ctrl+shift + L, to open the global search.2.I click alt+R to enable using regex. 3 I type the regex \bhref="([^>]*) so it shows me all the occurrences. 4. I click tab, which moves me to the next line called “Replace” and I type the $1 5.I click alt+enter, nothing happens 6. I click F1, then the dropdown list appears in the very top of the editor. 7 I type the “lower” (notice that there is > symbol added to the very beginning), and the message appears “No commands matching” 8.I click enter nothing happens. – GoldenAge Dec 08 '18 at 18:20
  • @GoldenAge vscode till now don't have support for that as far as i know.you can do this with notepad++ if files are large in number. the above mentioned works on one file at a time. – Code Maniac Dec 08 '18 at 18:41
  • Ok, how the solution would look like in the notepad++ then? I have to deal with thousands of files. – GoldenAge Dec 08 '18 at 18:59
  • @GoldenAge by using \L\E you can read more here http://docs.notepad-plus-plus.org/index.php/Regular_Expressions – Code Maniac Dec 09 '18 at 02:43