OneBite.Dev - Coding blog in a bite size

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)

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.

serie: "$1"

in the “Replace” field. The $1 is a backreference to the captured text inside the double quotes.

Visual screenshot find and replace regex in VS Code

vscode