2

This is find and replace issue in VS Code.

I want to find the following text in my Markdown file in VS Code:

## [](https://www.website.com/)Heading

and Replace with this text

 ## [Heading](https://www.website.com/)

So I use ## \[]\(.+?\)(.*) to find the expressions. But to replace with the above expression I typed this ## [$1]\(.+?\)(.*) but it doesn't works. How can I do this? Do regular expression doesn't work in replace?

  • 1
    At first make sure find part works as expected. For replace you can use reference to capture groups from find part, e.g.: ## [$1]($2) – Máté Juhász May 23 '18 at 6:21
2

In order to juggle the order of string parts one of the approaches is to first capture the whole string with all the respective parts.

Let’s look at the screenshot below. Every portion of the string is mapped to a numbered group. There are 6 of them in this example. Having all parts captured as groups allows precisely controlling the substitution section.

enter image description here

In VS Code it would look like this:
Find: (##)(\s+)(\[)(\])(\(.*?\))(Heading)
Replace: $1$2$3$6$4$5
INPUT
## [](https://www.website.com/)Heading
OUTPUT
## [Heading](https://www.website.com/)

Let's try something more general:
Find: (##)(\s+)(\[)(\])(\(.*?\))([^^]+)
Replace: $1$2$3$6$4$5
INPUT
## [](https://www.website.com/)Heading
## [](https://www.website2.com/)Heading2
## [](https://www.website3.com/)Heading3
OUTPUT
## [Heading](https://www.website.com/)
## [Heading2](https://www.website2.com/)
## [Heading3](https://www.website3.com/)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.