find and replace text in VS Code using regex
Here is how to find a certain text on VSCODE using regular expression and replace it with anything you want using part of the text before
If you need to change a certain part of your files in VS Code that has same patter but with different variables/text. VS Code has a replace all feature that support regular experssion.
Sample case
I want to replace
series: ["text"]
with
series: "text"
So I want to remove the bracket, but still maintain the same text uniquely for all pages
You can use the “Replace in Files” feature in VS Code with a regular expression to find and replace all occurrences of series: [“ANY TEXT HERE”] with serie: “ANY TEXT HERE”.
Here’s how to do it:
This is step by step (You can see the visual at the end)
- Press Ctrl+Shift+F on Windows/Linux or Cmd+Shift+F on Mac to open the “Search” panel. Click on the “.*” button to enable regular expression mode (or press Alt+R).
- Enter the following search pattern in the “Search” field:
series:\s*\["(.*?)"\]
explanation:
series: matches the literal string “series:”
\s* matches any number of whitespace characters (including zero)
”(.?)” matches any text inside double quotes, including the quotes themselves. The .? matches any characters (including line breaks) non-greedily, meaning it will stop as soon as it finds the closing quote.
- Enter
serie: "$1"
in the “Replace” field. The $1 is a backreference to the captured text inside the double quotes.
- Click on the ”…” button next to the “Search” field and select the folder(s) you want to search in.
- Click on the “Replace All” button (or press Ctrl+Shift+H on Windows/Linux or Cmd+Shift+H on Mac) to replace all occurrences of the pattern with the new string in all files in the selected folder(s). This should replace all instances of series: [“ANY TEXT HERE”] with serie: “ANY TEXT HERE” in all files in the selected folder(s).
Visual