0

In VS Code, is there a way to use a regular expression to find all statements which use var to declare a variable that assigns a value using require, and then replace just the var with const?

I have tried searching and I’m unable to find an answer.

This is the attempt at the regex (this part works): var[ 0-9a-zA-Z]*= require

It is the search and replace portion of just a part of the match where I am unsure how to do it, or if it is possible.

I’ve set up a StackBlitz.

In this screenshot, I’m trying to replace

var zlib = require('zlib');

by

const zlib = require('zlib');

Screenshot showing my attempt at regex with search and replace.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
WineGoddess
  • 412
  • 3
  • 11
  • 1
    Look into the usage of capturing groups in VS Code. – Sebastian Simon Mar 13 '21 at 20:04
  • @SebastianSimon thanks for your quick reply - it is about using the tool itself - i do not see why including an image of that caused a downvote. I did add a StackBlitz to make the question and problem more interactive. I appreciate the advice to look into capturing groups. – WineGoddess Mar 13 '21 at 20:12
  • @WineGoddess please refer to the below answer for the explanation to your question. Please add the exact snippet in the question in text format along with the image as it is easier for others to review your snippet and work on it locally. – dannysood Mar 13 '21 at 20:17
  • 1
    @WineGoddess i have added more description to the question as an suggested edit for the same – dannysood Mar 13 '21 at 20:21
  • Thank you @Danny - i did include the code via StackBlitz - is that an acceptable way to share the behavior of the code and tool? I didn't know what to search for. The comment from SebastiaSimon was helpful - and your answer much more concise and clear than my regexp. – WineGoddess Mar 13 '21 at 20:24
  • @WineGoddess i think it is but i am a newbie here as well. I think this is more of a meta discussion. Glad we could get this resolved. – dannysood Mar 13 '21 at 20:25
  • 1
    @WineGoddess Questions and answers have to be self-contained, so everything we need to know to answer a question or to apply an answer has to be inside the post itself. Links and images may only be _supplementary_. – Sebastian Simon Mar 13 '21 at 20:28
  • 1
    Find: `\bvar\b(?=.*\brequire\b)` replace : `const` See https://regex101.com/r/DvRKOB/1 – Mark Mar 13 '21 at 22:40
  • See also https://regex101.com/r/ma8Elf/1 Capture groups are not needed in this case. Use a positive lookahead instead - makes the replacement value simpler. – Mark Mar 13 '21 at 22:48

1 Answers1

2
in find section: var(.*?)=(.*?)require\(
in replace section: const$1=$2require(

Find and replace in action

Using round brackets around regex (.*) allows it to become a group which can be referred in the replace section of vscode by using group number $1,$2 and so on

Please read vscode documentation here for detailed explanation

Please read this answer for another example.

dannysood
  • 1,129
  • 3
  • 18
  • 36
  • 1
    Thank you! - the .* is cleaner and shorter as what I had as well. – WineGoddess Mar 13 '21 at 20:18
  • 1
    Although, `.*` may match too much. I’d recommend `.*?` instead. I’d _also_ recommend, simply going through all `var`s and replacing them by `const` or `let` as needed. – Sebastian Simon Mar 13 '21 at 20:25
  • @SebastianSimon updated the regex and screenshot based on your recommendation. – dannysood Mar 13 '21 at 20:29
  • 1
    See https://regex101.com/r/TD0Aj3/1 for a couple of cases that should not be matched, like `const somevar = require('zlib');` Word boundaries will help. – Mark Mar 13 '21 at 22:46