Languages
[Edit]
EN

VS Code - text replacing using regex groups

5 points
Created by:
Remy-Lebe
802

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

Simple steps:

  1. press Ctrl+F to open search popup window,
  2. click ▁* button in the search popup window to enable expression mode,
  3. click > button in the search popup window enable replace mode,
  4. in Find field write some expression with groups (use brackets),
    e.g. abc(\d+)abc(\d+)abc(\d+)
  5. in Replace field write some text using $1, $2, $3, etc. addressing groups,
    e.g. $1-$2-$3
  6. click Replace (Enter) button,
    e.g.
    abc123abc456abc789 text should be replaced to 123-456-789,
    abc1abc23abc456 text should be replaced to 1-23-456,
    etc.
  7. done!

Practical example

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

In the search popup window use the following values:

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

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

Screenshot:

Text replacing using regex groups in VS Code
Text replacing using regex groups in VS Code

Alternative titles

  1. VSCode - text replacing using regex groups
  2. VSCode - find and replace matching text using regex groups
  3. VS Code - find and replace matching text using regex groups
  4. Visual Studio Code - text replacing using regex groups
  5. Visual Studio Code - find and replace matching text using regex groups
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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