5

In the vs code find/replace editor widget, I'm using a named capture group (?<end>.*\s*). I'm then using ${end}in the replace but its just putting the literal text there instead of the captured contents. The unnamed capture groups are working as expected.

My regular expression works fine in Visual Studio 2019, but I'm not sure how to tweak the named capture group syntax for VS code.

default
  • 2,637
  • 21
  • 44
  • because VSC != Visual Studio, one is written in JavaScript the other in C++, in VSC use numeric group references like `$1` – rioV8 Sep 13 '21 at 16:11
  • @rioV8 - yep, I've understood that. In both VSC and Visual Studio, the $1 (ie unnamed group) syntax works. I'm specifically asking how to write the named group syntax for VSC. The Javascript documentation shows how to write it in javascript code (e.g. `${match.groups.firstname}`) but I'm not sure how to write it in the replace window in VSC. – default Sep 13 '21 at 16:18
  • can you give a link of this `Javascript documentation` – rioV8 Sep 13 '21 at 16:21
  • @rioV8 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges – default Sep 13 '21 at 17:05
  • in which version of Node is the supported and which version of Node is used by VSC – rioV8 Sep 13 '21 at 17:19
  • @rioV8 thank you THANK YOU for explaining it's $1 and not \1. – elucify May 20 '22 at 15:31

1 Answers1

7

vscode does not support named capture groups in the replacement, see and upvote Use regular expressions in Visual Studio: Named capture groups.

Also see a previous issue https://github.com/microsoft/vscode/issues/88793 that failed due to lack of upvotes.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Wow! I thought I'd gone crazy and forgotten how RegEx worked. – Simon_Weaver Jun 07 '22 at 04:03
  • Note: You can still put named groups in the regex itself - it's just that the replacement field only supports `$1`, `$2` and they'll still get populated even if you use `(?[a-z]+)` for the regex. – Simon_Weaver Jun 07 '22 at 04:04
  • ticket is closed, so thumbs up reaction is disabled. the ticket is in the backlog though? – starball Jun 14 '23 at 08:26