Languages
[Edit]
EN

VS Code - text replacing using regex groups

5 points
Created by:
Remy-Lebe
502

In this short article, we would like to show how to replace some text using groups in VS Code IDE.

Quick solution:

Address groups with $ notation:       $1  $2  $3, ...
Create groups with bracket notation: (  )(  )(  )


Example:

    Find field:
        ^import (.*) from "(.*)";

    Replace field:
        import $1 from '$2';

 

Simple steps:

  • Press Ctrl + F to open search window,
  • Click ▁* button in the search window to enable expression mode,
  • Click > button in the search window enable replace mode,
  • Create expression with groups / brackets
    e.g abc(\d+)abc(\d+)abc(\d+) ... and address groups with $1, $2, $3, ...

 

Practical example

In this section, we want to show how to replace double apostrophes with single apostrophes in ECMAScript imports.

Find field:
    ^import (.*) from "(.*)";

Replace field:
    import $1 from '$2';
VSCode search window with enabled replace mode.
VSCode search window with enabled replace mode.

Screenshot:

Text replacing using regex groups in VS Code
Text replacing using regex groups in VS Code
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join