skip to Main Content

In Visual Studio Code, in replace, I want to refer to what found in search with regex.

I thought this was done with $1, $2, etc. but I could not.

enter image description here

then:

enter image description here

What I want to achive is to get:

enter image description here

How can I refer in replace to what is found in search?

Note:
What I really aim is not the one in this example. It is only an example to explain what I need: refer to what is found. So, please don’t mind offer other ways to replace "spaces with ->"spaces<-.

2

Answers


  1. You can do this using the same syntax but in the search bar (Ctrl+Shift+F).

    edit: I was able to refer to the group this way:
    example

    Login or Signup to reply.
  2. In your example, nothing is "found" because you don’t make use of any capture groups in your pattern.

    If you need to refer to the whole match, use $0 in the Replace field.

    If you really want to use $1 instead for whatever reason, wrap your pattern in Find with parenthesis () to indicate it should be treated explicitly as a capture group.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search