Using VSCode find/replace, how would I find all strings within a file conforming to the following, and replace all dashes in some-text-that-looks-like-this
with underscores?
text: some-text-that-looks-like-this
{text: some-text-that-looks-like-this}
text: some-text-that-looks-like-this, some other text....
All strings of "some-text-that-looks-like-this"
start with "some-text"
.
I do some regex and I can find all instances of some-text-that-looks-like-this
, i.e.
(some-text-.*)b
But have no clue how to replace '-'
with '_'
at running time.
2
Answers
After that, do CtrlShiftP ->
Select All Occurences of Find Match
to select all matching words.Then click
Find in Selection
button (the button with three horizontal lines in the "find" popup).Then just type
-
in "find" and_
in "replace", and clickReplace All
.If the
text:
is actually in the text you are searching (and not just in your question to illustrate the issue) you can do this with a single regex:Find:
(?<=text: some-text[-w]*)-|(?<=text: )(some)-(text)
Replace:
$1_$2
See regex101 demo
This will work in vscode’s Find Widget (within the current file), but not in the Search view (across multiple files) due to the non-fixed length lookbehind.
When the first part of the regex matches (
(?<=text: some-text[-w]*)-
) there is no capture group 1 or 2 so the replace$1_$2
will only replace the matched-
‘s.When the second part of the regex (
(?<=text: )(some)-(text)
) matches then $1 =some
and $2 =text
.