visual studio code - VSCode regex find & replace submatch math?


Translate
%s@{fileID: \(213[0-9]*\)@\='{fileID: '.(submatch(1)-1900)@

I am using this regex search and replace command in vim to subtract a constant from each matching id.

I can do do the regex find in vscode but how can I reference the submatch for maths & replace? submatch(1) does not work in vscode?

Thanks.



All Answers
  • Translate

    Given a regular expression of (foobar) you can reference the first group using $1 and so on if you have more groups in the replace input field.


  • Translate

    To augment Benjamin's answer with an example:

    Find        Carrots(With)Dip(Are)Yummy
    Replace     Bananas$1Mustard$2Gross
    Result      BananasWithMustardAreGross
    

    Anything in the parentheses can be a regular expression.


  • Translate

    Just to add another example:

    I was replacing src attr in img html tags, but i needed to replace only the src and keep any text between the img declaration and src attribute.

    I used the find+replace tool (ctrl+h) as in the image: Find and replace