Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode wildcard Search and Replace Regex

I'm trying to do a project wide search and replace

from:

drivers[i].findElement(By.id("elementID")).click(); 

to:

findAndClick(driver[i], "elementID", true) 

The issue is the elementID can be anything so I'm trying to wildcard search and replace with what's in the wildcard?

enter image description here

like image 570
Batman Avatar asked Oct 17 '17 17:10

Batman


People also ask

Can you use regex in VSCode search?

Vscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.

How do you use find and Replace in VS code?

Find and Replace VS Code allows you to quickly find text and replace in the currently opened file. Press Ctrl+F to open the Find Widget in the editor, search results will be highlighted in the editor, overview ruler and minimap.

What is regex wildcard?

In regular expressions, the period ( . , also called "dot") is the wildcard pattern which matches any single character. Combined with the asterisk operator . * it will match any number of any characters.

What flavor of regex does VSCode use?

VSCode is using JavaScript-based regex engine, but it is not the same. You cannot use named capturing groups there. There is no specific documentation on the regex used in VSCode. However, if you have a look at the source code, you will see lots of JS code around.


1 Answers

You'll need to use .+? instead of * here since this uses regular expressions.

In regular expressions a dot . means "any character", the plus + means "one or more times", and the question mark ? after this means "try to match this as few as possible times" - which is useful so it won't keep matching past your quote marks

edit

To be clear though, you have to make a valid regex, which means you'll need to escape your parenthesis, dots, etc.

Here's the full solution

Find: drivers\[i\]\.findElement\(By\.id\("(.+?)"\)\)\.click\(\);

replace with: findAndClick(driver[i], "$1", true)

Note the added unescaped parentheses in there around the "wildcard" (.+) this creates a capture group in a regex, which is what translates to $1 in the replacement since it's the 1st capture group.

like image 92
FiniteLooper Avatar answered Sep 16 '22 20:09

FiniteLooper