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?
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.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With