EN
VS Code - text replacing using regex groups
5
points
In this short article, we would like to show how to replace some text using groups in VS Code IDE.
Simple steps:
- press
Ctrl+Fto open search popup window, - click
▁*button in the search popup window to enable expression mode, - click
>button in the search popup window enable replace mode, - in Find field write some expression with groups (use brackets),
e.g.abc(\d+)abc(\d+)abc(\d+) - in Replace field write some text using
$1,$2,$3, etc. addressing groups,
e.g.$1-$2-$3 - click
Replace (Enter)button,
e.g.
abc123abc456abc789text should be replaced to123-456-789,
abc1abc23abc456text should be replaced to1-23-456,
etc. - 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: